简体   繁体   中英

Django Redirecting from views.py to html page

I am using Django 1.11. I have developed some html pages & have views.py I have page called forgotpassword.html where-in I take EmailId of user. I find the respective security question & want to display it to user to enter the answerfor it. I have another page called getpassword.html for this. Here is the code: view.py

def forgotpassword(request):
   usercontemail = request.POST['useremail']
   #Get the securityquestion from Database  
   return redirect(request,'home/getpassword.html',{'squestion': squestion}) 


def getpassword(request): 
   #Display security question, get the answer from user & proceeed 

When user enters his email & hits submit I am able to see the input area for user to enter his security answer, which is coming from getpassword.html. But the url remains forgotpassword.html

So when the user enters his Security Answer, I am getting error of useremail cannot be blank. This is because of the url problem.

Can you please suggest workaround?

you have to do the redirection not render the template.

def forgotpassword(request):
  usercontemail = request.POST['useremail']
  #Get the securityquestion from Database 
  ## you can save the scurtiy question in session or you cn add the 
  ## question id in the url 
  return HttpResponseRedirect("url of the security question page") 

on the redirection of that, you gave check the method

 def getpassword(request):
    if request.METHOD == "GET":
       ## get question from session or from url 
       return redirect(request,'home/getpassword.html',{'squestion':squestion})
    else if request.METHOD == "POST":
          ##submit answer
         #Display security question, get the answer from user & proceeed 

another method just define the action in your form to URL of get password

I had a similar issue where I was updating a record and my render redirect was rendering the right page but my URL wasn't changing. What I wasn't aware of was that not only can you render redirect a specific page as you are doing but you can also render redirect a url which is specified in your url.py file. I found that by specifying a url.py declared url, it kind of sends the request through the normal loop of going to the url file then to the specified view file, passing any declared parameters along the way.

So in your code where you have

return redirect(request, 'home/getpassword.html', {'squestion': squestion })

you could try

#return redirect('app url.py:specified url of view', {*args}
return redirect('home:getpassword', {'squestion': squestion})

Just make sure your url.py file specifies the parameters you want to pass.

Check out this article on stackoverflow: Django return redirect() with parameters

and also the django docs reading the section on render redirect() https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM