Front also wrote about the simple logic of the relationship between the views, models, modules, address, and each cited explains. Usually see the most web pages will have a login, today we learned earlier in the first through the content, write a simple login page
Requirements Document
When we tested also the first to understand that demand, we here a first clear demand. Entered into a login page to enter the correct account password, go directly to the quiet blog page, if the account password is incorrect, an error will prompt the account password in the login page fails. Demand is very simple, but also the first to have the idea.
Thinking
1, first write a front page login.
2, account name was entered correctly judge
3, the account name is correct, then jump directly to the quiet blog page
4, account name wrong, then the account password prompt error
Small scale chopper
Did not talk much, we thought figured out, you can begin to write
log in page
The last time we wrote that the use of a simple login page (not written a front-end quiet, Cou Cou is east, west fight fight, to not look at ~ ~)
note:
1, here we have a user name and password must name = 'xxx' parameters later, for the back-end;
2, < form Action = "/ Login2 /" Method, = "POST" > Here we return to the page for the current page, if the password is wrong, also in this page, request method is POST
login2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <h1> <p style="text-align:center" font size="2">欢迎来到安静的博客:</p> </h1> <h1> <p style="text-align:center">请输出账号密码:</p> </h1> <form action="/login2/" method="post"> {% csrf_token %} <p style="text-align:center">用户:<input type="text" name="username" /><br /> </p> <p style="text-align:center">密码:<input type="password" name="password" /><br /> <input type="submit" value="登录" /> </form> </body> </html>
Judge right and wrong account password
There is no added database, first written account password directly died. (Follow-up will continue to optimize)
view.py
# Import redirect module, the requested page directly from django.shortcuts Import the render, redirect DEF Login2 (Request): IF request.method == ' the POST ' : username = request.POST.get ( ' username ' ) password = Request .POST.get ( ' password ' ) # Analyzing account password IF username == ' Anjing ' and password == ' 1234 ' : #When properly returned to the quiet blog page, or return to a specific page return redirect ( ' https://www.cnblogs.com/qican/ ' ) the else : # password is wrong to not write Pass return the render (Request, ' login2.html ' )
Written correspondence between the url
the urls.py from django.urls Import path from Anjing Import views # complete correspondence relationship between the urlpatterns = [ path ( ' Login2 / ' , views.login2), ]
Direct start debugging Django successful login results
We can see after entering the account password is correct, jump directly to the quiet of the blog, and if the login fails, continue to stay on this page.
Demand has been left with a wrong account password, the prompt return, how does this get? Thinking foregoing, return front-end data, we can define a key parameter in html, then an error is returned directly
Add error parameters
Wherein < P style = "Color: Red; text-align = left: Center" > {{}} ERROR_MSG </ P > means the presentation content error
Where the Django template language format: {{variable name}}
login2.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <h1> <p style="text-align:center" font size="2">欢迎来到安静的博客:</p> </h1> <h1> <p style="text-align:center">请输出账号密码:</p> </h1> <form action="/login2/" method="post"> {% csrf_token %} <p style="text-align:center">用户:<input type="text" name="username" /><br /> </p> <p style="text-align:center">密码:<input type="password" name="password" /><br /> <input type="submit" value="登录" /> <p style="color: red;text-align: center">{{ error_msg }}</p> </form> </body> </html>
View also need to add the contents of the error
the views.py from django.shortcuts Import the render, the redirect DEF Login2 (Request): # define an empty error ERROR_MSG = '' IF request.method == ' the POST ' : username = request.POST.get ( ' username ' ) password request.POST.get = ( ' password ' ) # Analyzing account password IF username == ' Anjing ' and password == ' 1234 ' : # Correct return to the quiet blog page, or return to a specific page return redirect ( ' https://www.cnblogs.com/qican/ ' ) the else : # If the account password is incorrect direct error ERROR_MSG = ' user name or password is incorrect Please re-enter ' return the render (request, ' login2.html ' , { ' ERROR_MSG ' : ERROR_MSG})
Restart Django, to see the effect of login failures
I write to you will find that our needs have all been completed.
new knowledge
Here the introduction of a new knowledge point is redirect module, we also used the front HttpResponse, render. So what's the difference between you three?
. 1, the HttpResponse application scenarios: Returns a string specified
2, the render application scenarios: returns an HTML file
3, redirect application scenarios: to jump to a specific page