橡皮擦,一個逗趣的互聯網高級網蟲,新的系列,讓我們一起進入 Django 世界,
已經完成的文章
- 滾雪球學 Python 第三輪,Python Web 之 Django 的世界
- 小手哆嗦一下,就能用 Python Django 實作一個微型博客系統
- Django 做個小后臺,細節在完善一點點,滾雪球學 Python 第三階段
- Django QuerySet 就學那么一點點,一點點就夠了
- 看完這篇博客,Python Django 你就學會一半了
- 讓我們一起開發【菜譜系統】吧,滾雪球學 Python 第三輪專案計劃
- 出現吧,Python Web 菜譜系統的首頁,不會前端技術,也能做
- 簡簡單單實作 Python Web 的登錄注冊頁面,還包含一半邏輯,
- Python Web 菜譜專案再次前進一步,從應用層了解內置用戶認證系統
- 菜譜系統小成階段,Python Web 領域終于攻占一個小山頭
- 銷售 小姐姐 給買家打分系統,用 Python Django 又整了一個花活
- 幫小姐姐打分系統的模型創建,滾雪球學 Python 第三輪第 12 篇
- 為了完成小姐姐安排的打分系統,又熬了一個小時的夜補充視圖與模板
滾雪球學 Python 第三輪
- 已經完成的文章
- 十四、打分頁面邏輯與實作
- 14.1 打分頁面功能實作
- 14.2 Django 知識點補充
- 14.3 客戶分數匯總頁面
- 14.4 本篇博客小節
十四、打分頁面邏輯與實作
14.1 打分頁面功能實作
本篇博客主要內容為實作打分系統的打分頁面,最終效果為簡版格式,
首先在 templates/ttt 目錄中新建一個 detail.html 檔案,輸入如下內容,
{% if customer %}
<h2>正在為客戶【{{customer.name}}】打分</h2>
{% if state == "success" %}
<h3>打分成功!</h3>
{% endif %}
<form method="post">
{% csrf_token %}
<label for="s">請打分:</label>
<select name="s" id="s">
<option value="10">10</option>
<option value="30">30</option>
<option value="50">50</option>
<option value="70">70</option>
<option value="90">90</option>
<option value="100">100</option>
</select>
<button type="submit">確定打分</button>
</form>
<a href="{% url 'scoring:index' %}">回傳串列</a>
{% else %}
<p>無客戶</p>
{% endif %}
該頁面由上一篇博客中超鏈接跳轉進入,

接下來修改 views.py,補充打分資料保存相關邏輯,
# 客戶詳情&打分頁面
def detail(request, _id):
# 用戶打分狀態資訊
state = None
# 渲染詳情頁面
customer = Customer.objects.get(_id=_id)
# 當用戶提交打分資訊
if request.method == "POST":
user_score_number = request.POST.get("s", 0)
user_score = Score(customer=customer, score=user_score_number)
user_score.save()
state = "success" # 表示打分成功
context = {
"customer": customer,
"state": state
}
return render(request, "ttt/detail.html", context)
在實作該頁面邏輯時,發現之前在打分模型中增加的 User 模型的外鍵出現問題,必須登錄才能保存資料,故修改 Score 模型如下,去除 User 模型外鍵,
class Score(models.Model):
# 自增主鍵
_id = models.AutoField(primary_key=True)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
score = models.IntegerField(default=0, verbose_name="分數")
# user_id = models.ForeignKey(User, on_delete=models.CASCADE)
模型修改之后,需要通過 migrate 遷移命令將修改保存到檔案中,
這些內容都修改完畢,就可以通過 http://127.0.0.1:8000/scoring/2/ 訪問打分頁面了,點擊確定打分,將資料發送到 views.py 中進行保存,

14.2 Django 知識點補充
在上文代碼中的 detail.html 頁面出現的 {% csrf_token %} 代碼塊,用于在 post 提交資料時,防止偽造跨站點請求,
request.POST 是一個類字典物件,用于通過關鍵字獲取提交的資料,例如上述代碼中,通過 request.POST.get("s", 0) 獲取 detail.html 頁面提交的下拉串列內容,
對于上述代碼,可以進行修改,當小姐姐打分之后,直接跳轉到客戶串列頁面,該編碼習慣可以防止用戶重復提交資料,建議牢記,
# 客戶詳情&打分頁面
def detail(request, _id):
# 渲染詳情頁面
customer = Customer.objects.get(_id=_id)
if request.method == "POST":
user_score_number = request.POST.get("s", 0)
user_score = Score(customer=customer, score=user_score_number)
user_score.save()
# 頁面跳轉
return HttpResponseRedirect(reverse("scoring:index"))
else:
context = {
"customer": customer
}
return render(request, "ttt/detail.html", context)
14.3 客戶分數匯總頁面
客戶分數查詢只需要用到 Django 中對符合條件的列求和即可,該部分細節知識點會在后續博客中進行講解,本文優先實作最終效果即可,
# 客戶分數查閱
def show_score(request, _id):
customer = Customer.objects.get(_id=_id)
total = Score.objects.filter(customer=customer).aggregate(nums=Sum('score'))
print(total['nums'])
return HttpResponse(f"客戶 {id} 的總分是 {total}")
上述代碼只會輸入一段文本,對其進行修改并匹配上相應的模板,代碼如下:
# 客戶分數查閱
def show_score(request, _id):
customer = Customer.objects.get(_id=_id)
total = Score.objects.filter(customer=customer).aggregate(nums=Sum('score'))
context = {
"customer":customer,
"total": total["nums"]
}
return render(request, "ttt/show_score.html", context)
show_score.html 檔案代碼如下:
{% if customer %}
<h2>{{ customer.name }} 的總得分是 {{ total }}</h2>
{% else %}
<p>無客戶</p>
{% endif %}
最終無效果的靜態頁面如下圖所示,

14.4 本篇博客小節
本篇博客實作了打分系統的評分頁面與客戶分數匯總展示頁面,希望你能在本篇博客中學到知識,
相關閱讀
- Python 爬蟲 100 例教程,超棒的爬蟲教程,立即訂閱吧
- Python 游戲世界(更新中,目標文章數 50+,現在訂閱,都是老粉)
- Python 爬蟲小課,精彩 9 講,只要 9 塊 9
今天是持續寫作的第 132 / 200 天,
如果你想跟博主建立親密關系,可以關注同名公眾號 夢想橡皮擦,近距離接觸一個逗趣的互聯網高級網蟲,
博主 ID:夢想橡皮擦,希望大家點贊、評論、收藏,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/275517.html
標籤:其他
上一篇:資料結構——紅玫瑰與白玫瑰之爭
下一篇:Divide by Zero 2021 and Codeforces Round #714 (Div. 2) ABC題解
