开发者

RequestContext on django redirect()

I'm wondering if there is any way to include the RequestContext in the django redirect function or any other context.

The thing is that I need to add a message after an object is created, but the messages framewotk requires a RequestContext to work or another context that returns the messages. How can I do to return a context?

My view:

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

                # This message does not work since there's no context.
                messages.info(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))


Storing a message doesn't require a RequestContext, it's only displaying it that does. In what way doesn't your code work? Your message should be added to the database and available to be displayed after the redirect.


I had the same question and just want to clarify that it 'just works'

def first_view(request)
    ...
    messages.info(request, "Some info message")
    ...
    return redirect('second_view')

def second_view(request)
    ...
    return render_to_response('template.html',{},context_instance=RequestContext(request))

    -> will correctly display the message
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜