django学习入门系列之第十点《案例 用户管理》

文章目录


展示用户列表

  • 方向
  1. 展示用户列表
    1. url
    2. 函数
      1. 获取用户所有的信息
      2. 基于HTML给他个渲染

views.py

python 复制代码
from django.shortcuts import render, HttpResponse, redirect


# Create your views here.


from app01.models import text_into
# 注意:函数默认要有个参数
def index(request):
    data_list = text_into.objects.all()
    print(data_list)
    return render(request, "text.html", {"data_list": data_list})

html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>密码</th>
        <th>年龄</th>
    </tr>
    </thead>
    <tbody>
    {% for foo in data_list %}

        <tr>
            <td>{{ foo.id }}</td>
            <td>{{ foo.name }}</td>
            <td>{{ foo.password }}</td>
            <td>{{ foo.age }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>


添加用户

  • 方向
    1. url
    2. 函数
      • GET,看到页面,输入内容。
      • POST,提交 ->写入到数据库。

views.py

python 复制代码
import requests
from django.shortcuts import render, HttpResponse, redirect


# Create your views here.


# 注意:函数默认要有个参数
def index(request):
    data_list = text_into.objects.all()
    print(data_list)
    return render(request, "text.html", {"data_list": data_list})


from app01.models import text_into


def add(request):
    if request.method == 'GET':
        return render(request, "add.html")

    user = request.POST.get("name")
    password = request.POST.get("password")
    age = request.POST.get("age")

    text_into.objects.create(name=user, password=password, age=age)

    # 如果是跳转自己的页面的话,可以不用写全
    # return redirect("http://127.0.0.1:8000/nima/")
    return redirect("/nima/")

add.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
</head>
<body>
<h1>用户提交</h1>
{#如果不写action,则默认向当前界面提交数据#}
<form method="post">
    {% csrf_token %}
    <input type="text" name="name" placeholder="用户名"/>
    <input type="password" name="password" placeholder="密码"/>
    <input type="text" name="age" placeholder="年龄"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

text

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>用户列表</h1>
<a href="/add/">添加用户</a>
<table border="1">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>密码</th>
        <th>年龄</th>
    </tr>
    </thead>
    <tbody>
    {% for foo in data_list %}

        <tr>
            <td>{{ foo.id }}</td>
            <td>{{ foo.name }}</td>
            <td>{{ foo.password }}</td>
            <td>{{ foo.age }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>

删除用户

url中?的作用

?说明

1、连接作用

http://www.xxx.com/Show.asp?id=77&nameid=2905210001&page=1
通过?来带参数,连接域名和参数,经常会用到。

2、清除缓存

http://www.xxxxx.com/index.html

http://www.xxxxx.com/index.html?test123123两个url
打开的页面一样,但是后面这个有问号,说明不调用缓存的内容,而认为是一个新地址,重新读取。

因为在做http请求的时候,如果浏览器检测到你的地址完全没变,会从缓存里读取先前请求过的数据,不再发送请求。有些时候是页面资源的加载,有些时候是API的get请求,都有可能。加上这个,会让浏览器认为这是一个新的地址,从而保证重新获取资源。

views.py

python 复制代码
import requests
from django.shortcuts import render, HttpResponse, redirect
from app01.models import text_into

# Create your views here.


# 注意:函数默认要有个参数
def index(request):
    data_list = text_into.objects.all()
    print(data_list)
    return render(request, "text.html", {"data_list": data_list})




def add(request):
    if request.method == 'GET':
        return render(request, "add.html")

    user = request.POST.get("name")
    password = request.POST.get("password")
    age = request.POST.get("age")

    text_into.objects.create(name=user, password=password, age=age)

    # 如果是跳转自己的页面的话,可以不用写全
    # return redirect("http://127.0.0.1:8000/nima/")

    return redirect("/nima/")


def delete(request):
    id = request.GET.get('uid')
    text_into.objects.filter(id=id).delete()
    return redirect("/nima/")

text.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>用户列表</h1>
<a href="/add/">添加用户</a>
<table border="1">
    <thead>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>密码</th>
        <th>年龄</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    {% for foo in data_list %}

        <tr>
            <td>{{ foo.id }}</td>
            <td>{{ foo.name }}</td>
            <td>{{ foo.password }}</td>
            <td>{{ foo.age }}</td>
            <td>
                {#  这里?的作用相当于把要删除id的值传递过来    #}
                <a href="http://127.0.0.1:8000/delete/?uid={{ foo.id }}">
                    删除
                </a>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>


</body>
</html>

往期回顾

1.【快速开发网站】
2.【浏览器能识别的标签1】
3.【浏览器能识别的标签2】
4.【浏览器能识别的标签3】
5.【浏览器能识别的标签4】
6.【案例1:用户注册】
7.【案例2:用户注册改进】
8.【快速了解 CSS】
9.【常用选择器概念讲解】
10.【CSS基础样式介绍1】
11.【CSS基础样式介绍2】
12.【CSS基础样式介绍3】
13.【CSS基础样式介绍3】
14.【案例 小米商城头标】
15.【案例 小米商城头标总结】
16.【案例 小米商城二级菜单】
17.【案例 商品推荐部分】
18.【伪类简单了解】
19.【position】
20.【案例 小米商城中app图标代码】
21.【边框及总结】
22.【BootSrap初了解】
23.【BootSrap的目录栏】
24.【BootSrap的栅格系统】
25.【案例 博客案例】
26.【案例 登录】
27.【案例 后台管理样例】
28.【图标】
29.【BootStrap依赖】
30.【javascript初了解】
31.【jJavaScript的变量】
32.【JavaScript的字符串类型】
33.【JavaScript的数组介绍】
34.【案例 动态数据】
35.【javascript 对象(字典)】
36.【案例 动态表格】
37.【Javascript的条件语句和函数】
38.【DOM初了解】
39.【DOM的事件了解】
40.【jQuery初了解】
41.【jQuery寻找标签】
42.【jQuery寻找标签2】
43.【jQuery寻找标签(间接寻找)】
44.【案例 菜单的切换】
45.【案例 只能打开一个菜单】
46.【jQuery 简单操作】
47.【案例 动态创建数据】
48.【案例 点击获取文本】
49.【案例 点击删除文本】
50.【案例 表格操作】
51.【案例 添加页面】
52.【初识MySQL】
53.【MySQL命令介绍一】
54.【MySQL命令介绍二】
55.【MySQL命令介绍三】
56.【案例:员工管理】
57.【案例 Flask+MySQL新增用户】
58.【案例 Flask+MySQL查询所有用户】
59.【初识 django】
60.【django的快速上手】
61.【django的模板语法】
62.【django的获取请求与响应】
63.【案例 用户登录】
64.【django中数据库操作】
65.【django中数据库操作--创建与删除表】
66.【django中数据库操作--操作表中的数据】

相关推荐
寂然如故2 小时前
Anaconda 安装
python
zhangbin_2372 小时前
【Python机器学习】NLP信息提取——命名实体与关系
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
985小水博一枚呀3 小时前
【梯度消失|梯度爆炸】Vanishing Gradient|Exploding Gradient——为什么我的卷积神经网络会不好呢?
人工智能·python·深度学习·神经网络·计算机视觉·cnn·numpy
全能全知者4 小时前
不废话简单易懂的Selenium 页面操作与切换
python·selenium·测试工具·网络爬虫
handsome2134 小时前
WSL中使用GPU加速AMBER MD--测试
笔记·学习
WZF-Sang5 小时前
Linux权限理解【Shell的理解】【linux权限的概念、管理、切换】【粘滞位理解】
linux·运维·服务器·开发语言·学习
狂飙的张兴发5 小时前
认知小文2《成功之路:习惯、学习与实践》
学习·考研·职场和发展·跳槽·学习方法·改行学it·高考
你可以自己看5 小时前
python的基础语法
开发语言·python
爱编程的小新☆5 小时前
C语言内存函数
c语言·开发语言·学习
尘浮生6 小时前
Java项目实战II基于Spring Boot的宠物商城网站设计与实现
java·开发语言·spring boot·后端·spring·maven·intellij-idea