I have a question: how to get the current path of the url. Let's say, I have 3 navigation bars, about , blog and contact page. In each page, I have facebook, twitter and a manual email a friend button. When I clicked the email a friend button, and the current URL is www.example.com/about, the current URL is now already www.example.com/emailafriend. How can I get the www.example/about? Also in blog and contact. Please help me. Thanks.
-
I don't understand how django is related to this . Can you make your situation a bit more clear . – Gautam Nov 29 '11 at 14:08
-
Please could you re-write the forth sentence, I don't understand what you're asking: *When I clicked the email a friend button, and the current URL is www.example.com/about, the current URL is now already www.example.com/emailafriend.* – MattH Nov 29 '11 at 14:08
-
Example, my current path right now is www.example.com/about, and there is an email a friend button, when clicked, the current path will be www.example.com/emailafriend. Is there a way that i can save the current path in a variable before moving on a different url? – david Nov 29 '11 at 14:16
3 Answers
How does your email a friend button work? Is it a django view that takes the current URL and emails it? If so, you don't want the "current" URL, which, as you note, is actually the email a friend URL. What you want to do is pass the URL you want to share as a URL parameter, ie:
/share?url=http://www.example.com/blog
Adding more info based on comments:
When I was referencing URL above, I was not referring to your django URL configuration. Let's take a step back.
On your About page you have a link to email a friend, right? That link is probably generated in your template, but it's the same on every page. Something like:
<a href="/emailafriend">Email a friend</a>
Instead of this, try this:
<a href="/emailafriend?url={{ request.get_full_path }}">Email a friend</a>
Now you need to make your email_a_friend view handle this. It can get the url via
request.get('url', '').
Some additional information:
You might want to escape the {{ request.get_full_path }} function so that it's escaped and URL safe, then you'll have to unescape it in your view. Once you get the URL back to your view, you can do as you please with it.
{{ request.get_full_path|urlencode }}

- 2,931
- 3
- 23
- 32
-
here's my sample url sir. url(r'^about/$', 'about', name='about'), url(r'emailafriend/$', 'email_a_friend', name='email_a_friend'), so when i am in the about page, there is an email a friend button, then if i'll click that button, the current url is now sample.com/emailafriend. I want is the url is still sample.com/about. Any idea sir? – david Nov 30 '11 at 10:04
Try using Relative URLs like for example From www.example.com/about
to get to www.example.com/email
use /email
. Using relative urls is the simplest solution .
Take a look at this.
-
I can't because I have many subdomains in my domain name. That should be visible. – david Nov 29 '11 at 14:10
It sounds like your want to get the referring URL (the URL that sent you to the current page). That is available to you in the request object, although it is not 100% reliable:
request.META['HTTP_REFERER']
See the documentation on HttpRequest objects for more information.

- 31,821
- 7
- 55
- 59
-
1After reading the question and the replies again, for a "email to a friend" feature, I would probably use the technique mentioned in Jason McClellan's answer rather than rely on the referrer. But you still have to not trust the URL however you got it. – Brian Neal Nov 29 '11 at 17:16