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.
Based on the definition of the
signup
method, you are only retrieving the value of thenext
parameter only for theGET
request. But when you are trying for aPOST
request, you do not retrieve the value of thenext
parameter. For this reason, the value of thenext
variable is set to""
and hence, the value of thesignup_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.