I've been tried many different things to solve this issue and none seem to work.
Essentially I am just trying to create a registiration page which contains 2 forms built from 2 models, the User model and my restaurant model (profile model). Every time I submit my form, I keep getting AttributeError 'tuple' object has no attribute 'get'. I thought it may have been my cuisine many to many field causing the issue as its the only thing that is a tuple I think. So I tried removing the cuisine aspect altogether but that didn't work. Heres my code. I can provide additional information if necessary. Appreciate any help very much
Traceback - dpaste.com/2CXS7MZ
Additional Information
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/register/
Django Version: 1.6.5
Exception Type: AttributeError
Exception Value:
'tuple' object has no attribute 'get'
Exception Location: C:\Python34\testapps\lib\site-packages\django\middleware\clickjacking.py in process_response, line 30
Python Executable: C:\Python34\testapps\Scripts\python.EXE
Python Version: 3.4.1
Models
class Cuisine(models.Model):
name = models.CharField(max_length=15, blank=False)
def __str__(self):
return self.name
#User Profile (The restaurant)
class Restaurant(models.Model):
#Main Test Field
name = models.CharField(max_length=25, blank=False)
user = models.OneToOneField(User)
cuisine = models.ManyToManyField(Cuisine, blank=True)
approved = models.BooleanField(default=False)
#Non Essential Fields
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
Forms
class UserSignUpForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'password', 'first_name', 'last_name', 'email',)
class RestaurantForm(forms.ModelForm):
cuisine = forms.ChoiceField(queryset=Cuisine.name.all())
name = forms.CharField(max_length=25)
class Meta:
model = Restaurant
exclude = ('user', 'approved',)
Views
def restaurant_register(request):
if request.user.is_authenticated():
return HttpResponse('Thank you for registering.')
if request.method == 'POST':
user_form = UserSignUpForm(request.POST)
restaurant_form = RestaurantForm(request.POST)
if user_form.is_valid() and restaurant_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = restaurant_form.save(commit=False)
profile.user = user
profile.save()
profile.save_m2m()
return render(request, '/auth/login.html')
else:
return user_form.errors, restaurant_form.errors
else:
user_form = UserSignUpForm()
restaurant_form = RestaurantForm()
return render(request, 'auth/register.html', {'restaurant_form':restaurant_form, 'user_form': user_form})
Template
{% extends "base.html" %}
{% block content %}
<form id="user_form" method="post" action="/auth/register/" enctype="multipart/form-data">
{{ user_form.errors }}
{{ restaurant_form.errors }}
{% csrf_token %}
{{ user_form.as_p }}
{{ restaurant_form.as_p }}
<input type="submit" name="submit" value="Register" />
</form>
{% endblock %}
URL
url(r'^register/$', views.restaurant_register, name='register'),
You are not returning a HttpResponse
here:
return user_form.errors, restaurant_form.errors
return this instead:
render(request, 'auth/register.html', {'restaurant_form':restaurant_form, 'user_form': user_form})
or rather, you should skip that whole else and return.