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

前端 未结 4 864
滥情空心
滥情空心 2020-12-04 16:36

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
相关标签:
4条回答
  • 2020-12-04 16:42

    In django view suppose you are not logged in but click on some content that content trigger some url like /board/2/new_topic then @login_required will redirect you to login page with this url

    http://localhost:8000/signin/?next=/boards/2/new_topic/

    so our aim is redirect to http://localhost:8000/boards/2/new_topic/ page after successful login so one line we will have to add

      if 'next' in request.GET:
            return redirect(request.GET['next'])
    

    then if it next is there then it will redirect according to that other normal redirect .

    Views.py :

    def signin(request):
    if request.method == "POST":
        user_login_form = UserLoginForm(request.POST)
        email = request.POST['email']
        password = request.POST['password']
        user = authenticate(request, email=email, password=password)
        if user and user.is_active:
            login(request, user)
            if 'next' in request.GET:
                return redirect(request.GET['next'])
            else:
                return redirect('home')
        else:
            return render(request, 'signin.html', context={'form': user_login_form})
    else:
        user_login_form = UserLoginForm()
        return render(request, 'signin.html', context={'form': user_login_form})
    
    0 讨论(0)
  • 2020-12-04 16:50

    100% working Example

    For Class Based View and Function:

    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 讨论(0)
  • 2020-12-04 17:00

    One of the way is using HTTP_REFERER header like as below:

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

    Not sure of cons of this!

    0 讨论(0)
  • 2020-12-04 17:00

    While the question and answer is old, I think it's lacking a few options. I have not find any cons with the methods, I would be happy to know if there are any?

    • request.path_info
    • request.get_full_path()
    • request.build_absolute_uri()

      from django.shortcuts import redirect
      
      redirect(request.path_info) # No query parameters
      
      redirect(request.build_absolute_uri()) # Keeps query parameters
      
      redirect(request.get_full_path()) # Keeps query parameters
      
    0 讨论(0)
提交回复
热议问题