gpt4 book ai didi

python - Django 表单保存问题 AttributeError 'tuple' object has no attribute 'get'

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:13 25 4
gpt4 key购买 nike

我已经尝试了很多不同的方法来解决这个问题,但似乎都没有用。

本质上,我只是想创建一个注册页面,其中包含由 2 个模型构建的 2 个表单,用户模型和我的餐厅模型(个人资料模型)。每次提交表单时,我都会收到 AttributeError 'tuple' object has no attribute 'get'。我认为这可能是我的多对多领域的 cooking 导致了这个问题,因为我认为它是唯一的元组。所以我尝试完全删除 cooking 方面,但那没有用。这是我的代码。如有必要,我可以提供更多信息。非常感谢任何帮助

追溯-dpaste.com/2CXS7MZ

附加信息

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

模型

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

表格

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',)

观看次数

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})

模板

{% 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(r'^register/$', views.restaurant_register, name='register'),

最佳答案

您不会在此处返回 HttpResponse:

return user_form.errors, restaurant_form.errors

改为返回:

render(request, 'auth/register.html', {'restaurant_form':restaurant_form, 'user_form': user_form})

或者更确切地说,您应该跳过整个其他部分并返回。

关于python - Django 表单保存问题 AttributeError 'tuple' object has no attribute 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com