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(以后有列表页,数据增、改页面,删除不需要单独html页面)
列表页
D:\mysite\polls\templates\polls\publisher_list.html
出版社列表
添加新的出版社
序号
ID
出版社名称
操作
{% for publisher in publisher_list %}
{{ forloop.counter }}
{{ publisher.id }}
{{ publisher.name }}
删除
编辑
{% endfor %}
#静态htm数据增页面
D:\mysite\polls\templates\polls\add_publisher.html
添加出版社
添加出版社
#静态html改页面
D:\mysite\polls\templates\polls\edit_publisher.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})
#删除函数
def delete_publisher(request):
print(request.GET)
print("=" * 120)
#1. 从GET请求的参数里面拿到将要删除的数据的ID值
del_id = request.GET.get("id", None) # 字典取值,娶不到默认为None
#如果能取到id值
if del_id:
# 去数据库删除当前id值的数据
#根据id值查找到数据
del_obj = models.Publisher.objects.get(id=del_id)
#删除
del_obj.delete()
#返回删除后的页面,跳转到出版社的列表页,查看删除是否成功
return redirect("/polls/publisher_list/")
else:
return HttpResponse("要删除的数据不存在!")
#编辑函数
def edit_publisher(request):
#用户修改完出版社的名字,点击提交按钮,给我发来新的出版社名字
if request.method == "POST":
print(request.POST)
#取新出版社名字
edit_id = request.POST.get("id")
new_name = request.POST.get("publisher_name")
#更新出版社
#根据id取到编辑的是哪个出版社
edit_publisher = models.Publisher.objects.get(id=edit_id)
edit_publisher.name = new_name
edit_publisher.save() # 把修改提交到数据库
#跳转出版社列表页,查看是否修改成功
return redirect("/polls/publisher_list/")
#从GET请求的URL中取到id参数
edit_id = request.GET.get("id")
if edit_id:
#获取到当前编辑的出版社对象
publisher_obj = models.Publisher.objects.get(id=edit_id)
return render(request, "polls/edit_publisher.html", {"publisher": publisher_obj})
else:
return HttpResponse("编辑的出版社不存在!")
访问列表页
http://127.0.0.1:8000/polls/publisher_list/
访问添加页
http://127.0.0.1:8000/polls/add_publisher/
访问编辑页
http://127.0.0.1:8000/polls/edit_publisher/?id=7