您好,登錄后才能下訂單哦!
1、django初始化配置
https://blog.51cto.com/yht1990/2382898
2、創(chuàng)建模型
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配置
主項(xiàng)目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),
]
應(yīng)用項(xiàng)目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'),
#添加數(shù)據(jù)
path('add_publisher/', views.add_publisher,name='add_publisher'),
#刪除數(shù)據(jù)
path('delete_publisher/', views.delete_publisher,name='delete_publisher'),
#path('edit_publisher/', views.edit_publisher,name='edit_publisher'),
]
五、靜態(tài)html(以后有列表頁,數(shù)據(jù)增、改頁面,刪除不需要單獨(dú)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>序號(hào)</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>
#靜態(tài)htm數(shù)據(jù)增頁面
D:\mysite\polls\templates\polls\add_publisher.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版社</title>
</head>
<body>
<h2>添加出版社</h2>
<!--url指提交的數(shù)據(jù)交給add_publisher函數(shù)處理,
polls為命名空間名字,綁定了add_publisher函數(shù),指執(zhí)行polls應(yīng)用下的add_publisher函數(shù)
-->
<form action="{% url 'polls:add_publisher' %}" method="post">
{% csrf_token %}
<input type="text" name="publisher_name">
<input type="submit" value="提交">
<p >{{ error }}</p>
</form>
</body>
</html>
#靜態(tài)html改頁面
D:\mysite\polls\templates\polls\edit_publisher.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>編輯出版社</title>
</head>
<body>
<h2>編輯出版社</h2>
<form action="/polls/edit_publisher/" method="post">
<input type="text" name="id" value="{{ publisher.id }}" >
<input type="text" name="publisher_name" value="{{ publisher.name }}">
<input type="submit" value="提交">
</form>
</body>
</html>
python后端
D:\mysite\polls\views.py
from django.shortcuts import HttpResponse, render, redirect
from polls import models
from .models import Publisher
#列表頁函數(shù)
def published_list(request):
ret = Publisher.objects.all().order_by("id")
return render(request,"polls/publisher_list.html",{"publisher_list": ret})
#添加函數(shù)
def add_publisher(request):
error_msg = ""
#如果是POST請求,我就取到用戶填寫的數(shù)據(jù)
print(request.method)
if request.method == "POST":
new_name = request.POST.get("publisher_name", None)
if new_name:
# 通過ORM去數(shù)據(jù)庫里新建一條記錄
Publisher.objects.create(name=new_name)
#返回訪問列表面,退出
return redirect("/polls/publisher_list/")
else:
#如果用戶post后沒有數(shù)據(jù)則設(shè)置變量
error_msg = "出版社名字不能為空!"
#如果是get請求訪問此頁面
return render(request, "polls/add_publisher.html", {"error": error_msg})
#刪除函數(shù)
def delete_publisher(request):
print(request.GET)
print("=" * 120)
#1. 從GET請求的參數(shù)里面拿到將要?jiǎng)h除的數(shù)據(jù)的ID值
del_id = request.GET.get("id", None) # 字典取值,娶不到默認(rèn)為None
#如果能取到id值
if del_id:
# 去數(shù)據(jù)庫刪除當(dāng)前id值的數(shù)據(jù)
#根據(jù)id值查找到數(shù)據(jù)
del_obj = models.Publisher.objects.get(id=del_id)
#刪除
del_obj.delete()
#返回刪除后的頁面,跳轉(zhuǎn)到出版社的列表頁,查看刪除是否成功
return redirect("/polls/publisher_list/")
else:
return HttpResponse("要?jiǎng)h除的數(shù)據(jù)不存在!")
#編輯函數(shù)
def edit_publisher(request):
#用戶修改完出版社的名字,點(diǎn)擊提交按鈕,給我發(fā)來新的出版社名字
if request.method == "POST":
print(request.POST)
#取新出版社名字
edit_id = request.POST.get("id")
new_name = request.POST.get("publisher_name")
#更新出版社
#根據(jù)id取到編輯的是哪個(gè)出版社
edit_publisher = models.Publisher.objects.get(id=edit_id)
edit_publisher.name = new_name
edit_publisher.save() # 把修改提交到數(shù)據(jù)庫
#跳轉(zhuǎn)出版社列表頁,查看是否修改成功
return redirect("/polls/publisher_list/")
#從GET請求的URL中取到id參數(shù)
edit_id = request.GET.get("id")
if edit_id:
#獲取到當(dāng)前編輯的出版社對象
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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。