Python web模版Django-10 增加登陆成功页面

在09文章中,在login_action函数中,处理登陆成功,我们直接用HttpResponese('login success!')。这一章将增加一个登陆页面 “event_manage.html", 用来替换 HttpResponese('login success!')。

step1: 回想07章中的Djanjo流程,第一步是添加一个url,这里添加  

path('event_manage/', views.event_manage),


step2:接下来到views.py中定义event_manage函数,

def event_manage(request):
    return render(request, "event_manage.html")

step3:函数需要调用event_manage.html,因此要到templates文件夹下定义event_manange.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Event Manage Page</title>
</head>
<body>
    <h1>Login Success!</h1>
</body>
</html>


step4: 至此,event_manage定义完毕,接下来到views.py下的login_action下改写 HttpResponese('login success!')。

这里用到一个新的方法 HttpResponseRedicect,先要引入到views.py;查找HttpResonseRedirect方法,其参数是完全特定的URL地址(比如,’http://www.yahoo.com/search/‘),或者是一个不包含域名的绝对路径地址(例如, ‘/search/’)。这里填入相对路径'/event_manage/'  ,注意两边都有'/'。

from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
def login_action(request):
    # request = HttpRequest(request)
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    if username == 'admin' and password == '123456':
        return HttpResponseRedirect('/event_manage/')
    else:
        return render(request, 'index.html', {'wronglyInput': '用户名或密码输入错误!'})

step5: 重新登陆,输入admin  123456,应该能login成功




猜你喜欢

转载自blog.csdn.net/pansc2004/article/details/80491141