开发者

Django returns error 500 for invalid forms

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)
              开发者_StackOverflow  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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜