Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass information using an HTTP redirect (in Django)

I have a view that accepts a form submission and updates a model.

After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.

How can I "pass" this message to the other page? HttpResponseRedirect only accepts a URL. I've seen this done before on other sites. How is this accomplished?

like image 887
user72389 Avatar asked Mar 01 '09 05:03

user72389


People also ask

Can we pass data with redirect in Django?

You can now perform a redirect with Django, either by using the redirect response classes HttpResponseRedirect and HttpResponsePermanentRedirect , or with the convenience function django.

How do you pass arguments in Django redirect?

shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.

What is HTTP Response redirect in Django?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.


4 Answers

This is a built-in feature of Django, called "messages"

See http://docs.djangoproject.com/en/dev/topics/auth/#messages

From the documentation:

A message is associated with a User. There's no concept of expiration or timestamps.

Messages are used by the Django admin after successful actions. For example, "The poll Foo was created successfully." is a message.

like image 118
S.Lott Avatar answered Oct 05 '22 14:10

S.Lott


You can use django-flashcookie app http://bitbucket.org/offline/django-flashcookie/wiki/Home

it can send multiple messages and have unlimited types of messages. Lets say you want one message type for warning and one for error messages, you can write

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    return HttpResponseRedirect("/")

or

def simple_action(request):
    ...
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")

or

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['error'] = 'something wrong'
    return HttpResponseRedirect("/")

or even

def simple_action(request):
    ...
    request.flash['notice'] = 'Hello World'
    request.flash['notice'] = 'Hello World 2'
    request.flash['error'] = 'something wrong'
    request.flash['error'] = 'something wrong 2'
    return HttpResponseRedirect("/")

and then in you template show it with

{% for message in flash.notice %}
    {{ message }}
{% endfor }}

or

{% for message in flash.notice %}
    {{ message }}
{% endfor }}
{% for message in flash.error %}
    {{ message }}
{% endfor }}
like image 35
user20955 Avatar answered Oct 05 '22 14:10

user20955


I liked the idea of using the message framework, but the example in the django documentation doesn't work for me in the context of the question above.

What really annoys me, is the line in the django docs:

If you're using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context.

which is incomprehensible to a newbie (like me) and needs to expanded upon, preferably with what those 2 options look like.

I was only able to find solutions that required rendering with RequestContext... which doesn't answer the question above.

I believe I've created a solution for the 2nd option below:

Hopefully this will help someone else.

== urls.py ==

from django.conf.urls.defaults import *
from views import *

urlpatterns = patterns('',
    (r'^$', main_page, { 'template_name': 'main_page.html', }, 'main_page'),
    (r'^test/$', test ),

== viewtest.py ==

from django.contrib import messages
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def test(request):
    messages.success( request, 'Test successful' )
    return HttpResponseRedirect( reverse('main_page') )

== viewmain.py ==

from django.contrib.messages import get_messages
from django.shortcuts import render_to_response

def main_page(request, template_name ):
    # create dictionary of items to be passed to the template
    c = { messages': get_messages( request ) }

    # render page
    return render_to_response( template_name, c, )

== main_page.html ==

{% block content %}
    {% if messages %}
    <div>
        {% for message in messages %}
            <h2 class="{{message.tag}}">{{ message.message }}</h2>
        {% endfor %}
    </div>
    {% endif %}
{% endblock %}
like image 26
idbill Avatar answered Oct 05 '22 15:10

idbill


I have read and checked all answers, and it seems to me that the way to go now is using the messaging framework. Some of the replies are fairly old and have probably been the right way at the time of the posting.

like image 25
Jan Detlefsen Avatar answered Oct 05 '22 15:10

Jan Detlefsen