前面也写了关于视图,模型,模块,地址之间的简单逻辑关系,也每一个都举了说明了。一般见到最多的web页面都会有登陆,今天咱们先经过前面学到的内容,简单的写一个登陆页面html
在咱们测试的时候也首先要了解到需求,这里咱们先明确一个需求。进入到一个登陆页面输入正确的帐号密码,直接跳转到安静博客页面,若是帐号密码错误,就在登陆页面报错提示帐号密码失败。需求很简单,可是也首先要有思路。前端
一、首先在写一个登陆的前端页面。web
二、判断输入的帐号名是否正确数据库
三、帐号名正确的话直接跳转到安静博客页面django
四、帐号名错误的话提示帐号密码错误后端
话很少说,咱们思路弄明白了,能够开始写了post
利用上次咱们写的那个简单的登陆页面(安静没有写过前端,都是东凑凑,西拼拼,先这样看着~~)测试
注意:优化
一、这里咱们用户名和密码必须后面有name='xxx'参数,进行返回给后端;url
二、 <form action="/login2/" method="post"> 咱们这里返回的页面为当前页面,若是密码错误的话,也停留在这个页面,请求方式为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>
这里没有添加数据库,就直接先把帐号密码写死了。(后续继续优化)
view.py
# 导入redirect模块,直接请求页面 from django.shortcuts import render,redirect def login2(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') # 判断帐号密码 if username == 'Anjing' and password == '1234': # 正确后返回到安静博客页面,也能够返回到指定页面 return redirect('https://www.cnblogs.com/qican/') else: #密码错误先不写 pass return render(request,'login2.html')
编写url对应关系
urls.py from django.urls import path from Anjing import views # 完成对应关系 urlpatterns = [ path('login2/', views.login2), ]
直接启动Django调试登陆成功效果
能够看到输入帐号密码正确后,直接跳转到了安静的博客,而后若是登陆失败的话,继续停留在这个页面。
需求已经就只剩下一个帐号密码错误,返回提示了,这个怎么弄呢?思考前面的内容,返回前端数据,咱们能够在html中定义一个参数项,而后报错直接返回
其中 <p style="color: red;text-align: center">{{ error_msg }}</p> 表示展示错误内容
其中Django模板语言格式: {{ 变量名 }}
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>
视图中也须要添加错误内容
views.py from django.shortcuts import render,redirect def login2(request): # 定义一个错误为空 error_msg = '' if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') # 判断帐号密码 if username == 'Anjing' and password == '1234': # 正确后返回到安静博客页面,也能够返回到指定页面 return redirect('https://www.cnblogs.com/qican/') else: # 若是帐号密码错误直接报错 error_msg = '用户名或密码错误,请从新输入' return render(request,'login2.html',{'error_msg':error_msg})
从新启动Django,查看登陆失败效果
写到这里会发现,咱们的需求已经所有完成了。
这里引入了一个新的知识点就是redirect模块,前面咱们还用到了HttpResponse,render。那么这3个之间有什么区别吗?
一、 HttpResponse的应用场景:返回一个指定的字符串
二、render的应用场景:返回一个HTML文件
三、 redirect的应用场景:跳转到指定的页面