django操作數據庫增刪改查
阿新 • • 發佈:2019-04-25
att log port 處理 頁面 pos get請求 style 項目 1、django初始化配置
https://blog.51cto.com/yht1990/2382898
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操作數據庫增刪改查