django操作数据库增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django操作数据库增删改查相关的知识,希望对你有一定的参考价值。
1、django初始化配置https://blog.51cto.com/yht1990/2382898
2、创建模型
D:\mysite\polls\models.py
from django.db import models
class Publisher(models.Model):
id = models.AutoField(primary_key=True) # 自增的ID主键
name = models.CharField(max_length=64, null=False)
3、建表
python manage.py makemigrations
python manage.py migrate
4、url配置
主项目url
D:\mysite\mysite\urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path(‘polls/‘,include(‘polls.urls‘)),
path(‘admin/‘, admin.site.urls),
]
应用项目url
D:\mysite\polls\urls.py
from django.urls import path
from . import views
#添加命名空间
app_name = ‘polls‘
urlpatterns = [
#访问列表页
path(‘publisher_list/‘, views.published_list,name=‘published_list‘),
#添加数据
path(‘add_publisher/‘, views.add_publisher,name=‘add_publisher‘),
#path(‘delete_publisher/‘, views.delete_publisher,name=‘delete_publisher‘),
#path(‘edit_publisher/‘, views.edit_publisher,name=‘edit_publisher‘),
]
五、静态html
列表页
D:\mysite\polls\templates\polls\publisher_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>出版社列表</title>
</head>
<body>
<a href="/polls/add_publisher/">添加新的出版社</a>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>ID</th>
<th>出版社名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for publisher in publisher_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ publisher.id }}</td>
<td>{{ publisher.name }}</td>
<td>
<a href="/delete_publisher/?id={{ publisher.id }}">删除</a>
<a href="/edit_publisher/?id={{ publisher.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
#数据库增
D:\mysite\polls\templates\polls\add_publisher.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版社</title>
</head>
<body>
<h1>添加出版社</h1>
<!--url指提交的数据交给add_publisher函数处理,
polls为命名空间名字,绑定了add_publisher函数,指执行polls应用下的add_publisher函数
-->
<form action="{% url ‘polls:add_publisher‘ %}" method="post">
{% csrf_token %}
<input type="text" name="publisher_name">
<input type="submit" value="提交">
<p style="color: red">{{ error }}</p>
</form>
</body>
</html>
python后端
D:\mysite\polls\views.py
from django.shortcuts import HttpResponse, render, redirect
#from polls import models
from .models import Publisher
列表页
def published_list(request):
ret = Publisher.objects.all().order_by("id")
return render(request,"polls/publisher_list.html",{"publisher_list": ret})
#添加新的出版社
def add_publisher(request):
error_msg = ""
#如果是POST请求,我就取到用户填写的数据
print(request.method)
if request.method == "POST":
new_name = request.POST.get("publisher_name", None)
if new_name:
# 通过ORM去数据库里新建一条记录
Publisher.objects.create(name=new_name)
#返回访问列表面,退出
return redirect("/polls/publisher_list/")
else:
#如果用户post后没有数据则设置变量
error_msg = "出版社名字不能为空!"
#如果是get请求访问此页面
return render(request, "polls/add_publisher.html", {"error": error_msg})
访问列表页
http://127.0.0.1:8000/polls/publisher_list/
访问添加页
http://127.0.0.1:8000/polls/add_publisher/
以上是关于django操作数据库增删改查的主要内容,如果未能解决你的问题,请参考以下文章
五 Django框架,models.py模块,数据库操作——表类容的增删改查