Tutorial :Django request paths



Question:

I've been working through an issue with my django project. The issue is I've got one project, which will retrieve data for users of different clients. I need to know 'from where' a viewer is coming from (request.path [my original solution]). I've been looking at a number of different options which sound close to what I want to do, but I'm not sure what the best option is, not having done this before.

My first option was to add a url in the urls.py with a 'tag' or 'keyword' then look for that tag/keyword in the request.path, which I'd add as a session key. Then go onto get the data.

Something else I started looking at was the sites framework. After reading through the documentation, I'm still confused how sites actually works, so I'm not sure if this is the right option.

Another solution talked about using middleware, this came up in connection with the research into using the sites framework.

And then yet another talked about doing this in apache.

Could some one help point me in the right direction?

Cheers,

T


Solution:1

If you need to know from which URL came your user to your currrent page you should check the REFERER http header, available in request.META.get('HTTP_REFERER'). See http://docs.djangoproject.com/en/1.2/ref/request-response/#ref-request-response for more informations.

Be careful though, the referer meta is not mandatory and could be missing due to private browsing or direct access to the page from the URL bar.


Solution:2

It's not completely clear from your question, but if you're asking for the URL that the user was on before coming to the current page, you probably want request.META['HTTP_REFERRER'].

Edit after comment

That would be a very bad idea. Global variables are not safe given that you potentially have multiple requests being processed at the same time. The referrer is already available from the request, which can be accessed in all views and templates, so I don't know what else a middleware would give you.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »