Respond to the redirected URL in Django

In a form submission script, the form presents a message for "/ submit". I want to redirect the user to "/ sucess" for success and pass the rendering of some message to the template with the new URL. How to do it in Django? render_to_response does not redirect, and HttpResponseRedirect does not render templates.

+3
source share
6 answers

If your success page needs a dynamic message, you need to convey it somehow. You can do this by using the GET parameters in the URL you are redirecting to, for example return HttpResponseRedirect('/success/?msg=My+success+message+here'), or by setting some values ​​in the session that the success representation may display.

+3
source

A response from Daniel Roseman is potentially dangerous by opening your site for XSS vulnerabilities.

Make sure you separate the html tags from any post if you should do it this way.

A better way would be to redirect to / success /? msg = ok and have a dictionary in your dictionary:

{ 'ok': 'Your success message' }
+9
source

, : , ( ) dict. render_to_response .

, , .

: :

if anyError:
   dict = {"message" : {"error" : "The error message"}}
else:
   dict = {"message" : {"success" :"The success message"}}

:

   {% if message %}
      {% if message.error %}
         {{ message.error }}
      {% else %}
         {{ message.success }}
      {% endif %}
   {% endif %}

,

+1

, . , /. , "", ; . , , . , , POST .

. , - .

0

, Django , . "post_save_redirect". Django. , , , , URL- "/user/add". "/user/1" " № 1". , , , . Django python, Django, , , .

0

I would recommend using the Django messaging environment, which is used for one-time notifications.

https://docs.djangoproject.com/en/1.8/ref/contrib/messages/

0
source

Source: https://habr.com/ru/post/1725406/


All Articles