zoukankan      html  css  js  c++  java
  • 用户登录以及跳转后台管理页面Django

    做一个登录后的跳转页面

    这个home页面,我们用来管理用户的信息,我们不能写死在页面上,需要在后台生成数据,这个怎么做呢?

    1、定义一个home页面  home.html

    2、设置home 的urls

    3、编写后台view.py代码

    home页面

    我们先写一个home.html的页面,在模板中编写,templates中创建home.html

    <body style="margin: 0 auto;">
        <div style="height: 48px;background: #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>
                {% for row in user_list %}
                    <tr>
                        <td>{{ row.username }}</td>
                        <td>{{ row.email }}</td>
                        <td>{{ row.gender }}</td>
                    </tr>
                {% endfor %}
            </table>
        </div>
    </body>
    

     注意:上面用的是模板语言,且我们需要添加内容提交到后台。

    设置home 的urls

    说明:我们需要设置urls,当用户访问的时候,跳转到我们的后台处理

    编写后台view.py代码

    USER_LIST中有一条基础数据,for循环再自动添加5条数据 ,在模板中需要用for循环读取

    USER_LIST = [
        {'username': "dada", 'email': "asdf@123.456", "gender": "girl"},
    ]
    
    
    for index in range(5):
        temp = {'username':"user" + str(index),'email':"abc"+ str(index)+"@123.com","gender":"man"}
        USER_LIST.append(temp)
    
    # home页面处理的请求
    def home(request):
        return render(request, "home.html", {"user_list": USER_LIST})
    

    模板语言的for循环

    django的模板中是支持for循环的,所以当我们需要有很多数据的时候,是需要用到for循环的,语法如下:

    {% for 元素名 in 列表名 %}
          {{ 元素值}}  
    {% endfor %}
     
    #如果是字典的话,是:{{元素.key值}},比如:{{row.username}},不是{{row["username"]}}

    运行,登录成功跳转,读取所有的用户名

     view.py扩展

    处理post请求

    # home页面处理的请求
    def home(request):
        if request.method == "POST":
            user = request.POST.get("username")
            email = request.POST.get("email")
            gender = request.POST.get("gender")
            temp = {'username': user, 'email': email, "gender": gender}
            USER_LIST.append(temp)
        return render(request, "home.html", {"user_list": USER_LIST})
    

     页面上添加一条数据,每次提交都会新加一条,因为已经把这个值加到了全局变量USER_LIST里

     但是,一旦重启,全局变量会被清空。

  • 相关阅读:
    用JAVA发送一个XML格式的HTTP请求
    LR 测试http协议xml格式数据接口
    软件测试术语
    linux学习笔记
    接口测试文章整理
    InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
    zTree更新自定义标签>>>
    C# 各类常见Exception 异常信息
    C# 调用存储过程 Sql Server存储过程 存储过程报错,程序中的try
    SQL Server 2014 清除用户名和密码
  • 原文地址:https://www.cnblogs.com/meihan/p/12510293.html
Copyright © 2011-2022 走看看