Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the previous URL from a POST in Django

I have a Post model that requires a certain category before being added to the database, and I want the category to be generated automatically. Clicking the addPost button takes you to a different page and so the category will be determined by taking a part of the previous page URL.

Is there a way to get the previous page URL as a string?

I have added my AddPost button here.

<aside class="addPost">
    <article>
        <form action="/Forum/addPost">
            <input type="submit" name="submit" value="Add Post"/>
        </form>
    </article>
</aside>
like image 285
tryingtolearn Avatar asked Sep 06 '25 14:09

tryingtolearn


1 Answers

You can do that by using request.META['HTTP_REFERER'], but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict. So be careful and make sure that you are using .get() notation instead.

# Returns None if user came from another website
request.META.get('HTTP_REFERER')

Note: I gave this answer when Django 1.10 was an actual release. I'm not working with Django anymore, so I can't tell if this applies to Django 2

like image 181
Viktor Avatar answered Sep 09 '25 03:09

Viktor