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中数据库操作--操作表中的数据】

相关推荐
Jackyzhe5 小时前
Flink学习笔记:整体架构
笔记·flink
江沉晚呤时5 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
徒 花5 小时前
初级网安作业笔记1
笔记
kfepiza5 小时前
Debian-10编译安装Mysql-5.7.44 笔记250706
linux·数据库·笔记·mysql·debian·bash
西西西仓鼠5 小时前
python学习打卡:DAY 40 训练和测试的规范写法
学习
FreeBuf_5 小时前
黄金旋律IAB组织利用暴露的ASP.NET机器密钥实施未授权访问
网络·后端·asp.net
Magnetic_h5 小时前
【iOS】方法与消息底层分析
笔记·学习·macos·ios·objective-c·cocoa
今天背单词了吗9806 小时前
算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·算法·牛顿迭代法
电脑能手6 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
Edward-tan6 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python