Implementation method:
1, you can define a basic page access path First, for example: http://127.0.0.1:8000/index/define the index path
In URLs
1 urlpatterns = [2 3 url (r'^index/$', Views.index),4 5 ]
2, also need to create a index.html page
<HTMLxmlns= "http://www.w3.org/1999/html"><Head> <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"/> <title>Landing page</title></Head><Body><formMethod= "POST"Action= "/login_action/"> <!--Create a form for data submission and submit a form using post, defined as Login_action (login) process -- <inputname= "username"type= "text"placeholder= "User name"><BR> <!--input tag definition text box, data type-- <inputname= "Password"type= "Password"placeholder= "Password"><BR>{{Error}}<BR> <!--the double curly braces here can be used to display the contents specified by the function- - <ButtonID= "BTN"type= "Submit">Landing</Button>{% Csrf_token%} <!--to prevent CSRF attacks--
</form></Body></HTML>
3, need a function to connect the URL and HTML.
Define views.py
1 fromDjango.shortcutsImportRender2 fromDjango.httpImportHttpResponse3 fromDjango.httpImportHttpresponseredirect #这三个模块为常用模块4 #Create your views here.5 6 defIndex (Request):7 returnRender (Request,'index.html')8 deflogin_action (Request):9 ifRequest.method = ='POST':#determine if it is a post submission methodTenUsername = Request. Post.get ('username',"')#Use the Post.get () method to get the username and password entered OnePassword =request. Post.get ('Password',"') A - ifUsername = ='Admin' andPassword = ='123':#determine if the user name and password are correct - returnHttpresponseredirect ('/event_manage/')#If it is correct, (call another function here, implement the login Success page independently, using the Httpresponseredirect () method to implement the Else: - returnRender (Request,'index.html',{'Error':'username or password eror'})#Incorrect , an error message is displayed at the error label via the render (request, "index.html") method - - defEvent_manage (Request):#This function defines a prompt page for a successful page + - #username =request. Cookies.get (' user ', ') #读取浏览器cookie + returnRender (Request,"event_manage.html")#{"User": Username}) #在上面的函数判断用户名密码正确后在显示该页面, specify to event_manage.html, switch to a new HTML page
The methods used include
Render ()
Post.get ()
Httpresponseredirect ()
HttpResponse ()
Be familiar with the use of them
The Login_action () function, which corresponds to the submission process of the form defined in index.html, we submit the data in this process and judge the data,
Event_manage () function to open a new HTML page
5, create success page Event_manage. Html
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Success</title></Head><Body> <H1>Hello, Intruder.</H1></Body></HTML>
The functions defined in views.py should be defined in the urls.py path name can be customized, but corresponding to the function name, here in order to correspond with the corresponding function
6, define the path of the above two functions
Urlpatterns = [ url (r'^index/$', views.index), URL (r') ^login_action/$', views.login_action), #登陆过程 url (r' ^event_manage/$', Views.event_manage), #成功的页面]
Summarize this whole process
First, we access the base landing page through http://127.0.0.1:8000/index/
Enter the user name password, click the Submit button, this procedure (login_action) calls the Login_action () function {and jumps to http://127.0.0.1:8000/login_action/} —————— Make a judgment----------correct---------jump to http://127.0.0.1:8000/event_manage/immediately and show event_manage.html.
The entire implementation process
First create a path, the corresponding HTML page
and bind them together through a function.
The process of implementing form content submission defines a function for working with data, and a function for specifying a jump to another page
In short, the views.py is defined in the processing of various data processing in HTML, data judgment, page jump
These functions, defined at the same time, are links to each process method, and they should be created in urls.py.
Python Django Simple Login Implementation