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

相关推荐
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
CSDN_PBB3 小时前
[STM32 - 野火] - - - 固件库学习笔记 - - - 十五.设置FLASH的读写保护及解除
笔记·stm32·学习
m0_748256143 小时前
SpringBoot
java·spring boot·后端
多想和从前一样4 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
涛粒子6 小时前
Spring Bean 生命周期的执行流程
java·后端·spring
小喵要摸鱼6 小时前
【Pytorch 库】自定义数据集相关的类
pytorch·python
bdawn6 小时前
深度集成DeepSeek大模型:WebSocket流式聊天实现
python·websocket·openai·api·实时聊天·deepseek大模型·流式输出
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt