david
david

Reputation: 299

How to get the previous URL in Django

I'll give the step by step info.

Let's say we're in the about page, the URL is example.com/about. There's an email a friend button, when clicked, the URL is example.com/emailafriend. Then when I clicked the submit button, the referal URL will be submitted is example.com/emailafriend. Question, how to get the about page URL? BTW, I'm using request.META['HTTP_REFERER'].

Upvotes: 14

Views: 10375

Answers (3)

Mathieu Dhondt
Mathieu Dhondt

Reputation: 8914

You could work with the request path attribute and add that to your form's action attribute, like so:

<form method="post" action="/tell-a-friend?return_url={{ request.path }}">
...
</form>

Then, in your tell-a-friend view, HttpResponseRedirect to the return_url query parameter.

Upvotes: 13

Denis Kabalkin
Denis Kabalkin

Reputation: 508

Maybe simply pass sample.com/about as GET or POST param? Or even through session.

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 40193

It looks like maybe you are using APPEND_SLASH which will do a redirect if you don't have a slash. Try changing the button link to sample.com/emailafriend/, note the ending slash.

Upvotes: 1

Related Questions