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

后端 未结 11 842
北恋
北恋 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:23

    Take a look at Django's messages framework. http://docs.djangoproject.com/en/dev/ref/contrib/messages/#ref-contrib-messages

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

    While all suggestions so far work, I would suggest going with Ry4an's (pass it in the request URL) - just change the actual text to a coded text within a predefined set of text messages.

    Two advantages here:

    1. Less chance of something hacking through your scrubbing of bad content
    2. You can localize your messages later if needed.

    The other cookie related methods.. well, they don't work if the browser doesn't support cookies, and are slightly more expensive.. But only slightly. They're indeed cleaner to the eye.

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

    The solution used by Pydev UA is the less intrusive and can be used without modifying almost nothing in your code. When you pass the message, you can update your context in the view that handles the message and in your template you can show it.

    I used the same approach, but instead passing a simple text, passed a dict with the information in useful fields for me. Then in the view, updated context as well and then returned the rendered template with the updated context.

    Simple, effective and very unobstrusive.

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

    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.

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

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