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

后端 未结 11 813
北恋
北恋 2021-02-01 19:22

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 su

相关标签:
11条回答
  • 2021-02-01 20:02

    I think this code should work for you

    request.user.message_set.create(message="This is some message")
    return http.HttpResponseRedirect('/url')
    
    0 讨论(0)
  • 2021-02-01 20:10

    You could also have the redirect url be the path to an already parameterized view.

    urls.py:

    (r'^some/path/(?P<field_name>\w+)/$', direct_to_template,
        {'template': 'field_updated_message.html',
        },
        'url-name'
    ),
    

    views.py:

    HttpResponseRedirect( reverse('url-name', args=(myfieldname,)) )
    

    Note that args= needs to take a tuple.

    0 讨论(0)
  • 2021-02-01 20:20

    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.

    0 讨论(0)
  • 2021-02-01 20:21

    There is a lot of solutions

    1 Use Django-trunk version - it support sending messages to Anonymous Users

    2 Sessions

    def view1(request):
        request.session['message'] = 'Hello view2!'
        return HttpResponseRedirect('/view2/')
    
    
    def view2(request):
        return HttpResponse(request.session['message'])
    

    3 redirect with param

    return HttpResponseRedirect('/view2/?message=Hello+view2')
    

    4 Cookies

    0 讨论(0)
  • 2021-02-01 20:22

    Can you just pass the message as a query param oon the URL to which you're redirecting? It's not terribly RESTy, but it ought to work:

    return HttpResponseRedirect('/polls/%s/results/?message=Updated" % p.id)
    

    and have that view check for a message param, scrub it for nasties, and display it at the top.

    0 讨论(0)
  • 2021-02-01 20:23

    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 %}
    
    0 讨论(0)
提交回复
热议问题