How do I pass template context information when using HttpResponseRedirect in Django?

前端 未结 7 1814
我在风中等你
我在风中等你 2020-12-05 04:26

I have a form that redirects to the same page after a user enters information (so that they can continue entering information). If the form submission is successful, I\'m r

7条回答
  •  温柔的废话
    2020-12-05 04:52

    For the sake of completion and future reference, you can now use the messages framework. After you install it:

    views.py

    from django.contrib import messages
    
    def view(request):
      # your code
      messages.success(request, "Your data has been saved!")
      HttpResponseRedirect(request.path)
    

    template.html

    {% if messages %}
    
      {% for message in messages %} {{ message }} {% endfor %}
    {% endif %}

提交回复
热议问题