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

相关推荐
trayvontang11 分钟前
Python虚拟环境与包管理工具(uv、Conda)
python·conda·uv·虚拟环境·miniconda·miniforge
伊织code14 分钟前
pdfminer.six
python·pdf·图片·提取·文本·pdfminer·pdfminer.six
楼田莉子17 分钟前
C++算法题目分享:二叉搜索树相关的习题
数据结构·c++·学习·算法·leetcode·面试
十一102432 分钟前
FX10/20 (CYUSB401X)开发笔记5 固件架构
笔记
hqxstudying40 分钟前
JAVA项目中邮件发送功能
java·开发语言·python·邮件
小兔兔吃萝卜1 小时前
Spring 创建 Bean 的 8 种主要方式
java·后端·spring
FakeOccupational1 小时前
【电路笔记 通信】AXI4-Lite协议 FPGA实现 & Valid-Ready Handshake 握手协议
笔记·fpga开发
Q_Q5110082851 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
Java中文社群1 小时前
26届双非上岸记!快手之战~
java·后端·面试