Django 对于无效表单返回错误 500

发布于 2024-12-09 12:21:34 字数 2487 浏览 1 评论 0原文

可能这是另一个用户错误,但我遇到了一个奇怪的问题。

我正在开发一个平台,如果你不搞乱它,一切都会很好。现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-12-16 12:21:34

我认为您必须在保存表单之前调用 is_valid 。

I think you have to call is_valid on a form before saving it.

情深缘浅 2024-12-16 12:21:34
ef_uncommited = entity_forms.save(commit=False)
            for ef in ef_uncommited:
                ef.space = space
                ef.save()

在它说的那一行,ef.space = space。空间指的是什么?您还没有定义名称“space”,所以我不确定 ef.space 被设置为什么,它是字符串“space”还是变量。

ef_uncommited = entity_forms.save(commit=False)
            for ef in ef_uncommited:
                ef.space = space
                ef.save()

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.

爱你不解释 2024-12-16 12:21:34

尝试查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文