How to pass context data with django redirect function?
Asked Answered
A

5

6

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?

Armyworm answered 2/1, 2016 at 8:34 Comment(0)
C
5

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.

Convalescent answered 2/1, 2016 at 12:15 Comment(0)
M
0

You can redirect with session using request.session["key"] as shown below:

# "views.py"

from django.shortcuts import redirect

def my_view(request): 
            # Here
    request.session["message"] = "some_message"
    return redirect('/orders')
# "index.html"

{{ request.session.message }} {# some_message #}
Missi answered 31/7, 2022 at 18:44 Comment(0)
R
0

Perhaps a bit bootleg, but works for my use-case. Using session but with a change. Once the redirect goes through, the session is deleted and the variable is passed down to the template as a context.

class OAuthLoginErrorView(socialaccount_views.LoginErrorView):
    def get(self, request):
        request.session["error_login_error"] = True
        return redirect(reverse("account_login"))
class LoginView(allauth_views.LoginView):
    def get(self, request):
        errors = list(filter(lambda arg: arg.startswith("error_"), request.session.keys()))
        if errors:
            for error in errors:
                del request.session[error]

            return render(
                request,
                self.template_name,
                context={
                    **self.template_settings,
                    **{error: True for error in errors},
                },
            )

        return super().get(request)
{% if error_login_error %}
    <div class="text-sm text-red-500 font-center">There was an error logging-in.</div>
{% endif %}

Works for what I need.

Radbun answered 16/5, 2023 at 0:36 Comment(0)
T
-1

If you are using http redirect (the case you mentioned), you need to pass your arguments through url's query string:

redirect('/orders?message=some_message')

Another ways is that you call other view with your custom parameter, which is not recommended generally.

Edit: You can also use django.sessions to make central request-based messaging.

Thermit answered 2/1, 2016 at 8:43 Comment(0)
T
-1

I mean you can always use the get_context_data method to do that. This method will always sent the data from the view to the template.

Tattler answered 23/11, 2018 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.