Next parameter in url not showing but is printing correctly in django

163 views Asked by At

This is my signup view:

def signup(request):
    next = request.GET.get('next', '')
    print(next)
    if request.user.is_authenticated:
        return redirect('/')
    else:
        if request.method == "POST":
            first_name=request.POST['first_name']
            email=request.POST['email']
            password=request.POST['password']
            cpassword=request.POST['cpassword']
            signup_uri = f'/signup?next={next}'

            if password==cpassword:
                if User.objects.filter(email=email).exists():
                    messages.info(request,'Email already in use')
                    return redirect(signup_uri)
                elif User.objects.filter(mobile=mobile).exists():
                    messages.info(request,'Mobile Number already in use')
                    return redirect(signup_uri)
                else:
                    user=User.objects.create_user(first_name=first_name,email=email,password=password)
                    user.save();
                    return redirect(f'/login?next={next}')
            else:
                messages.info(request,'Passwords not matching')
                return redirect('signup_uri')
        
        else:
            return render(request,'signup.html')

The problem I am facing is that when I am printing next under def signup it is printing it correctly but when it has to redirect it redirects without showing anything as next in url. That is signup_uri = f'/signup?next={next}' and return redirect(f'/login?next={next}') are showing the {next} as empty.What could be the reason?Any help would be appriciated.

1

There are 1 answers

6
sayem On

Based on the definition of the signup method, you are only retrieving the value of the next parameter only for the GET request. But when you are trying for a POST request, you do not retrieve the value of the next parameter. For this reason, the value of the next variable is set to "" and hence, the value of the signup_uri variable is being set as "/signup?next=" as well as for the login redirecting url ("/login?next=") too. In order to get rid of this problem, your code should be similar to as follows.

def signup(request):
    next = request.GET.get('next', '')
    print(next)
    if request.user.is_authenticated:
        return redirect('/')
    else:
        if request.method == "POST":
            first_name = request.POST['first_name']
            email = request.POST['email']
            password = request.POST['password']
            cpassword = request.POST['cpassword']
            next = request.POST.get("next", "")
            signup_uri = f'/signup?next={next}'

            if password == cpassword:
                if User.objects.filter(email=email).exists():
                    messages.info(request,'Email already in use')
                    return redirect(signup_uri)
                elif User.objects.filter(mobile=mobile).exists():
                    messages.info(request,'Mobile Number already in use')
                    return redirect(signup_uri)
                else:
                    user = User.objects.create_user(first_name=first_name, email=email, password=password)
                    user.save();
                    return redirect(f'/login?next={next}')
            else:
                messages.info(request, 'Passwords not matching')
                return redirect('signup_uri')
    
        else:
            return render(request, 'signup.html')