Redirect / return to the same (previous) page in Django?

What are the options when you want to return the user to the same page in Django and what are the pros / cons of each?

Methods I know:

  • HTTP_REFERER
  • GET parameter containing the previous URL
  • Session data to save previous URL

Are there any others?

+51
django django-views
Oct. 06
source share
3 answers

One way is to use the HTTP_REFERER header, as shown below:

 from django.http import HttpResponseRedirect def someview(request): ... return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 

Not sure about the downsides of this!

+99
Oct 06 '12 at 10:14
source share

Although the question and answer are outdated, I think he lacks a few options. I did not find any disadvantages with the methods, I would be glad to know if there are any?

+8
Oct 18 '17 at 14:48
source share

100% working example

To view based on classes and functions:

 from django.http import HttpResponseRedirect ... return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 

or

 from django.http import HttpResponseRedirect ... return HttpResponseRedirect(self.request.META.get('HTTP_REFERER')) 

Example -

 class TaskNotificationReadAllView(generic.View): def get(self, request, *args, **kwargs): TaskNotification.objects.filter(assigned_to=request.user).update(read=True) print(request.META.get('HTTP_REFERER')) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) 
0
Jun 24 '19 at 9:27
source share



All Articles