美文网首页
django实例之用户登陆界面

django实例之用户登陆界面

作者: 后来者2016 | 来源:发表于2017-12-12 00:42 被阅读0次

    views.py

    #coding:utf-8
    from django.shortcuts import render,redirect
    from django.http import HttpResponse
    
    from django import forms
    
    # Create your views here.
    
    USER_LIST = [
        {'username':'chinablue','email':'124@126.com','gender':'man'}
    ]
    for index in range(20):
        tmp = {'username':'chinablue'+str(index),'email':'124@126.com','gender':'man'}
        USER_LIST.append(tmp)
    
    def login(request):
        # 获取用户提交方法, request包含客户端所有信息
        # print request.method
    
        # 接收到的数据以字典形式接收
        error_msg = ""
        if request.method == "POST":
            # 获取用户通过post提交的数据,request.POST看做一个字典
            # 方式1:
            # user = request.POST['username']
            # pwd = request.POST['pwd']
            # print user,pwd
            # 方式2:key不存在也不会报错
            user = request.POST.get('username',None)
            pwd = request.POST.get('pwd', None)
            print user, pwd
            # 获取到值后,需要做判断。值正确跳转,不正确则有提示
    
    
            if user == 'root' and pwd == '123':
                # 跳转到首页
                return redirect('/home/')
            else:
                # 用户名密码不匹配 error_msg来处理
                error_msg = "用户名或密码错误"
        return render(request,'login.html',{'error_msg':error_msg})
    
    def home(request):
        if request.method == "POST":
            # 获取用户提交的数据
            u = request.POST.get('username')
            e = request.POST.get('email')
            g = request.POST.get('gender')
            tmp = {'username': u, 'email': e, 'gender': g}
            USER_LIST.append(tmp)
        return render(request,'home.html',{'user_list':USER_LIST})
    

    login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>login</title>
        <style>
            label{
                width: 80px;
                text-align: right;
    
                display: inline-block;
    
            }
        </style>
        <!--<link rel="stylesheet" href="/static/commons.css">-->
    </head>
    <body>
        <!--action="/login"表示将表单提交到当前的url -->
        <form action="/login/" method="post">
            <p>
            <label for="user">用户名:</label>
            <!--设置name,使得后台有获取数据    -->
            <input id="user" type="text" name="username">
            </p>
            <p>
            <label for="passwd">密码:</label>
            <input id="passwd" type="password" name="pwd">
            </p>
            <input type="submit" value="提交">
            <span style="color: red">{{ error_msg }}</span>
        </form>
    
    
    </body>
    </html>
    

    home.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>home</title>
    </head>
    <body style="margin: 0">
        <div style="height: 48px;background-color: #dddddd"></div>
        <div>
            <form action="/home/" method="post">
                <input type="text" name="username" placeholder="用户名">
                <input type="text" name="email" placeholder="邮箱">
                <input type="text" name="gender" placeholder="性别">
                <input type="submit" value="添加">
            </form>
        </div>
        <div>
            <table border="1">
                {% for row in user_list %}
                <tr>
                    <td>{{ row.username }}</td>
                    <td>{{ row.email  }}</td>
                    <td>{{ row.gender  }}</td>
                </tr>
                {% endfor %}
            </table>
        </div>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:django实例之用户登陆界面

          本文链接:https://www.haomeiwen.com/subject/asqgixtx.html