码迷,mamicode.com
首页 > 其他好文 > 详细

django从零开始-视图

时间:2017-12-05 17:37:21      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:turn   har   创建用户   view   required   password   else   用户名   col   

1.处理的登录请求

views文章中添加登录函数login_action

def login_action(request):
    if request.method == POST:
        username = request.POST.get(username,‘‘)
        password = request.POST.get(password, ‘‘)
        if username == admin and password == admin:
            return HttpResponse(登录成功)
        else:
            return  render(request,index.html,{error:用户名或者密码错误})

     添加url路由

  

 url(r^login_action$,views.login_action),

2.登录成功页面

创建event_manage.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布会管理</title>
</head>
<body>
<h1>
    <center>登录成功</center>
</h1>
</body>
</html>

改写views.py的登录函数,返回一个页面

def login_action(request):
    if request.method == POST:
        username = request.POST.get(username,‘‘)
        password = request.POST.get(password, ‘‘)
        if username == admin and password == admin:
            return  HttpResponseRedirect(/event_manage/)
            #return render(request,‘event_manage.html‘)#BUILD2
            #return HttpResponse(‘登录成功‘)
        else:
            return  render(request,index.html,{error:用户名或者密码错误})


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

改下url路由

  url(r^event_manage/$,views.event_manage),

3.cookie and  seeion

在这里我们使用seeion

  1. 更改试图函数技术分享图片

     

     

  2. 编辑html文件接受session技术分享图片

     

  3. 数据库迁移
     python .\manage.py migrate

     

 4.认证系统

  •   登录admin后台
    • 创建用户
       python .\manage.py createsuperuser

       

  • 引用认证登录
    • 修改login函数
      • def login_action(request):
            if request.method == POST:
                username = request.POST.get(username,‘‘)
                password = request.POST.get(password, ‘‘)
                user = auth.authenticate(username = username, password = password)
                if user is not None:
                    auth.login(request,user)
                #if username == ‘admin‘ and password == ‘admin‘:
                
                    response =  HttpResponseRedirect(/event_manage/)
                    request.session[user] = username
                    return  response
                    #return render(request,‘event_manage.html‘)#BUILD2
                    #return HttpResponse(‘登录成功‘)
                else:
                    return  render(request,index.html,{error:用户名或者密码错误})

         

  • 使用装饰器 关窗
    • 使用自带login_required装饰登录后才能展示的函数
    • 修改url路由

 

 

 

 

django从零开始-视图

标签:turn   har   创建用户   view   required   password   else   用户名   col   

原文地址:http://www.cnblogs.com/mrwuzs/p/7988090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!