I have function which redirect to a URL.
return redirect('/orders')
The URL /orders
has some context data which is being passed to it.
I want to pass some additional data along with data from the URL's function in view like:
return redirect('/orders', {'message':'some_message'})
I tried like this according to documentation:
return redirect('/orders', message='some_message')
But it didn't passed any data back to html. So how to pass data with redirect?
I would use session variables in order to pass some context through a redirect. It is about the only way to do it outside of passing them as part of the url and it is the recommended django option.
Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .
There is a difference between the two: In the case of HttpResponseRedirect the first argument can only be a url . redirect which will ultimately return a HttpResponseRedirect can accept a model , view , or url as it's "to" argument. So it is a little more flexible in what it can "redirect" to.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
If its just a small item, like ?status=2
it can be added to the URL in the usual way. (small aside: careful, it may have a negative impact of how search engines spider your web site, i.e. create duplicate content issues)
However, for your example, passing an entire "message" string to the next page, I would recommend to do it the same way Django's Messages framework does it: by using Django's Session engine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With