Django 对于无效表单返回错误 500
可能这是另一个用户错误,但我遇到了一个奇怪的问题。
我正在开发一个平台,如果你不搞乱它,一切都会很好。现在我正在使用 DEBUG=False 和错误的用户行为进行测试,例如提交不完整的表单(将必填字段留空)。
问题是,如果用户发送无效表单,django (1.3.1) 应该自动返回表单错误列表,但会为应用程序中的每个表单返回 500 错误。
我认为我的代码很好,但我不确定。
views.py
@permission_required('spaces.add_space')
def create_space(request):
"""
Returns a SpaceForm form to fill with data to create a new space. There
is an attached EntityFormset to save the entities related to the space. Only
site administrators are allowed to create spaces.
:attributes: - space_form: empty SpaceForm instance
- entity_forms: empty EntityFormSet
:rtype: Space object, multiple entity objects.
:context: form, entityformset
"""
space_form = SpaceForm(request.POST or None, request.FILES or None)
entity_forms = EntityFormSet(request.POST or None, request.FILES or None,
queryset=Entity.objects.none())
if request.user.is_staff:
if request.method == 'POST':
space_form_uncommited = space_form.save(commit=False)
space_form_uncommited.author = request.user
if space_form.is_valid() and entity_forms.is_valid():
new_space = space_form_uncommited.save()
space = get_object_or_404(Space, name=space_form_uncommited.name)
ef_uncommited = entity_forms.save(commit=False)
for ef in ef_uncommited:
ef.space = space
ef.save()
# We add the created spaces to the user allowed spaces
request.user.profile.spaces.add(space)
#messages.success(request, _('Space %s created successfully.') % space.name)
return redirect('/spaces/' + space.url)
return render_to_response('spaces/space_add.html',
{'form': space_form,
'entityformset': entity_forms},
context_instance=RequestContext(request))
else:
return render_to_response('not_allowed.html',
context_instance=RequestContext(request))
模板太大,无法粘贴到此处,您可以在 dpaste 中看到它,
我不明白为什么django 返回 500 错误而不是显示错误列表。有人可以帮助我吗?
完整的源代码(毕竟是 GPL)位于 GitHub 上。该模块位于 src/e_cidadania/apps/spaces 上
Probably this is another user error, but I'm having a weird problem with this.
I'm developing a platform and everything works fine if you don't mess with it. Now I'm testing with DEBUG=False and wrong user behaviour like submitting incomplete forms (leaving required fields empty).
The thing is that django (1.3.1) is supposed to return the form errors list automatically if the user sends an invalid form, but instead returns a 500 error for every form in the application.
I think my code is fine, but I'm not sure.
views.py
@permission_required('spaces.add_space')
def create_space(request):
"""
Returns a SpaceForm form to fill with data to create a new space. There
is an attached EntityFormset to save the entities related to the space. Only
site administrators are allowed to create spaces.
:attributes: - space_form: empty SpaceForm instance
- entity_forms: empty EntityFormSet
:rtype: Space object, multiple entity objects.
:context: form, entityformset
"""
space_form = SpaceForm(request.POST or None, request.FILES or None)
entity_forms = EntityFormSet(request.POST or None, request.FILES or None,
queryset=Entity.objects.none())
if request.user.is_staff:
if request.method == 'POST':
space_form_uncommited = space_form.save(commit=False)
space_form_uncommited.author = request.user
if space_form.is_valid() and entity_forms.is_valid():
new_space = space_form_uncommited.save()
space = get_object_or_404(Space, name=space_form_uncommited.name)
ef_uncommited = entity_forms.save(commit=False)
for ef in ef_uncommited:
ef.space = space
ef.save()
# We add the created spaces to the user allowed spaces
request.user.profile.spaces.add(space)
#messages.success(request, _('Space %s created successfully.') % space.name)
return redirect('/spaces/' + space.url)
return render_to_response('spaces/space_add.html',
{'form': space_form,
'entityformset': entity_forms},
context_instance=RequestContext(request))
else:
return render_to_response('not_allowed.html',
context_instance=RequestContext(request))
The template is too big to paste here, you can see it in dpaste
I don't get why django is returning the 500 error instead of showing the error list. Can anybody help me?
The complete source code (it's GPL after all) is on GitHub. This modules is on src/e_cidadania/apps/spaces
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您必须在保存表单之前调用 is_valid 。
I think you have to call is_valid on a form before saving it.
在它说的那一行,ef.space = space。空间指的是什么?您还没有定义名称“space”,所以我不确定 ef.space 被设置为什么,它是字符串“space”还是变量。
On the line where it says, ef.space = space. What is space referring to? You haven't defined a name "space" so I'm not sure what ef.space is being set to, is it meant to be the string 'space' or a variable.
尝试查看 HTTP 服务器日志以查看错误是什么。这可能有助于缩小问题范围。如果您使用 Apache,日志默认为 /var/log/apache/-error.log。
当发生意外/无法解释的 500 错误时,您通常可以在 Web 服务器错误日志中找到更多详细信息。
Try viewing the HTTP server log to see what the error is. This might help to narrow down the issue. If you are using Apache, the log defaults as /var/log/apache/-error.log.
When unexpected/unexplained 500 errors occur, you can often find more details in the web server error log.