61π
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
9π
You can get the referring URL by using request.META.HTTP_REFERER
More info here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
- [Django]-Python: Memory leak debugging
- [Django]-Can I use Django F() objects with string concatenation?
- [Django]-Django Rest Framework: Disable field update after object is created
4π
I canβt answer @tryingtolearn comment, but for future people, you can use request.META['HTTP_REFERER']
- [Django]-Which Model Field to use in Django to store longitude and latitude values?
- [Django]-Django testing model with ImageField
- [Django]-Strings won't be translated in Django using format function available in Python 2.7
3π
Instead of adding it to your context, then passing it to the template, you can place it in your template directly with:
<a href="{{ request.META.HTTP_REFERER }}">Return</a>
- [Django]-Django Manager Chaining
- [Django]-Circular dependency in Django Rest Framework serializers
- [Django]-Django, Turbo Gears, Web2Py, which is better for what?
2π
A much more reliable method would be to explicitly pass the category in the URL of the Add Post button.
- [Django]-Django, how to remove the blank choice from the choicefield in modelform?
- [Django]-Django: Error: You don't have permission to access that port
- [Django]-How to get GET request values in Django Views?
1π
You can get the previous url in "views.py" as shown below:
# "views.py"
from django.shortcuts import render
def test(request):
pre_url = request.META.get('HTTP_REFERER') # Here
return render(request, 'test/index.html')
You can also get the previous url in Django Template as shown below:
# "index.html"
{{ request.META.HTTP_REFERER }}
- [Django]-How to get user permissions?
- [Django]-Is there a way to pass a variable to an 'extended' template in Django?
- [Django]-Check if model field exists in Django