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

相关推荐
玩电脑的辣条哥1 小时前
一台服务器已经有个python3.11版本了,如何手动安装 Python 3.10,两个版本共存
服务器·python·python3.11
wqfhenanxc1 小时前
Mixing C++ and Rust for Fun and Profit 阅读笔记
c++·笔记·rust
weixin_307779131 小时前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
豆沙沙包?2 小时前
8.学习笔记-Maven进阶(P82-P89)
笔记·学习·maven
海天一色y2 小时前
Pycharm(十六)面向对象进阶
ide·python·pycharm
??? Meggie2 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
Minyy112 小时前
SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
xml·java·spring boot·后端·spring·mybatis·logback
XIE3923 小时前
Browser-use使用教程
python
醉暮天3 小时前
4.25学习——文件上传之00截断
学习
武昌库里写JAVA3 小时前
39.剖析无处不在的数据结构
java·vue.js·spring boot·课程设计·宠物管理