Django: getting the previous url

I have a Post model that requires a certain category before adding to the database, and I want the category to be generated automatically. When you click the addPost button, you get to another page, and therefore the category will be determined by the acceptance of part of the previous page URL.

Is there a way to get the previous page url?

thanks

Edit: I added my AddPost button here.

<aside class="addPost"> <article> <form action="/Forum/addPost"> <input type="submit" name="submit" value="Add Post"/> </form> </article> </aside> 
+8
python url django
source share
4 answers

You can do this with self.request.META['HTTP_REFERER'] , but it will exist only if your tab on the previous page was from your site, otherwise there will be no HTTP_REFERER in the META dict . So be careful and make sure you use .get() notation .get() .

 # Returns None if user came from another website referer = self.request.META.get('HTTP_REFERER') 
+11
source share

You can get the reference URL using request.META.HTTP_REFERER

More information here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META

+7
source share

I can not answer the comment @tryingtolearn, but for future people you can use request.META['HTTP_REFERER']

+3
source share

A more reliable way would be to explicitly pass the category to the URL of the Add Message button.

+1
source share

All Articles