刪除討論主題
新增路徑規則
開啟 forum/topic/urls.py
,新增第 9 行規則:
urlpatterns = [
path('', TopicList.as_view(), name='topic_list'),
path('new/', TopicNew.as_view(), name='topic_new'),
path('<int:pk>/', TopicView.as_view(), name='topic_view'),
path('<int:tid>/reply/', TopicReply.as_view(), name='topic_reply'),
path('<int:tid>/delete/', TopicDelete.as_view(), name='topic_delete'),
]
新增處理視圖
開啟 forum/topic/views.py
,增加以下程式碼:
from django.contrib.auth.mixins import PermissionRequiredMixin
class TopicDelete(PermissionRequiredMixin, DeleteView):
permission_required = 'topic.delete_topic'
model = Topic
template_name = 'confirm_delete.html'
def get_success_url(self):
return reverse('topic_list')
新增頁面範本
新增刪除頁面範本檔案 forum/pagetmpl/confirm_delete.html
:
{% extends "base.html" %}
{% block content %}
<h1>刪除記錄</h1>
<p class="alert alert-danger">
您確定要刪除這筆記錄嗎? <br/>
{{ object }}
</p>
<form action="" method="POST">
{% csrf_token %}
<input type="submit" class="btn btn-danger" value="是的,我要刪除" />
</form>
{% endblock %}
更新主題檢視範本
修改 forum/pagetmpl/topic/topic_detail.html
:
{% extends "base.html" %}
{% block content %}
<div class="card">
<div class="card-header d-md-flex justify-content-between">
<div class="h3">{{ topic.subject }}</div>
<div class="text-muted">
<small><i class="fas fa-user"></i> {{ topic.author }}</small>
<small><i class="fas fa-clock"></i> {{ topic.created|date:"Y/m/d H:i" }}</small>
</div>
</div>
<div class="card-body">
{% if perms.topic.delete_topic %}
<a href="{% url 'topic_delete' topic.id %}" class="btn btn-sm btn-danger float-right">刪除主題</a>
{% endif %}
{{ topic.content|linebreaks }}
</div>
{% if user.is_authenticated %}
<div class="card-footer">
<a href="{% url 'topic_reply' topic.id %}" class="btn btn-primary">回覆討論</a>
</div>
{% endif %}
</div>
<hr/>
<ul class="list-group">
{% for reply in reply_list %}
<li class="list-group-item d-md-flex justify-content-between">
<div class="ml-2">{{ reply.content|linebreaks }}</div>
<small class="text-muted">
<i class="fas fa-user"></i> {{ reply.author }}
<i class="fas fa-clock"></i> {{ reply.created|date:"Y/m/d H:i" }}
</small>
</li>
{% endfor %}
</ul>
{% endblock %}
刪除回覆意見
開啟 forum/topic/urls.py
,增加第 10 行路徑規則:
urlpatterns = [
path('', TopicList.as_view(), name='topic_list'),
path('new/', TopicNew.as_view(), name='topic_new'),
path('<int:pk>/', TopicView.as_view(), name='topic_view'),
path('<int:tid>/reply/', TopicReply.as_view(), name='topic_reply'),
path('<int:pk>/delete/', TopicDelete.as_view(), name='topic_delete'),
path('reply/<int:pk>/delete/', ReplyDelete.as_view(), name='reply_delete'),
]
開啟 forum/topic/views.py
,增加以下視圖類別來處理刪除回覆的請求:
class ReplyDelete(PermissionRequiredMixin, DeleteView):
permission_required = 'topic.delete_reply'
model = Reply
template_name = 'confirm_delete.html'
def get_success_url(self):
reply = self.get_object()
return reverse('topic_view', args=[reply.topic.id])
- 第 69 行,在
DeleteView
的衍生類別裡可透過其 get_object()
方法來取得欲刪除的那筆紀錄
更新檢視討論主題頁面範本
開啟 forum/pagetmpl/topic/topic_detail.html
,修改為以下程式碼:
{% extends "base.html" %}
{% block content %}
<div class="card">
<div class="card-header d-md-flex justify-content-between">
<div class="h3">{{ topic.subject }}</div>
<div class="text-muted">
<small><i class="fas fa-user"></i> {{ topic.author }}</small>
<small><i class="fas fa-clock"></i> {{ topic.created|date:"Y/m/d H:i" }}</small>
</div>
</div>
<div class="card-body">
{% if perms.topic.delete_topic %}
<a href="{% url 'topic_delete' topic.id %}" class="btn btn-sm btn-danger float-right">刪除主題</a>
{% endif %}
{{ topic.content|linebreaks }}
</div>
{% if user.is_authenticated %}
<div class="card-footer">
<a href="{% url 'topic_reply' topic.id %}" class="btn btn-primary">回覆討論</a>
</div>
{% endif %}
</div>
<hr/>
<ul class="list-group">
{% for reply in reply_list %}
<li class="list-group-item d-md-flex justify-content-between">
<div class="ml-2">{{ reply.content|linebreaks }}</div>
<small class="text-muted">
<i class="fas fa-user"></i> {{ reply.author }}
<i class="fas fa-clock"></i> {{ reply.created|date:"Y/m/d H:i" }}
{% if perms.topic.delete_reply %}
<a href="{% url 'reply_delete' reply.id %}" class="btn btn-sm btn-danger">刪除回覆</a>
{% endif %}
</small>
</li>
{% endfor %}
</ul>
{% endblock %}
刪除討論主題
新增路徑規則
開啟
forum/topic/urls.py
,新增第 9 行規則:新增處理視圖
開啟
forum/topic/views.py
,增加以下程式碼:新增頁面範本
新增刪除頁面範本檔案
forum/pagetmpl/confirm_delete.html
:更新主題檢視範本
修改
forum/pagetmpl/topic/topic_detail.html
:刪除回覆意見
開啟
forum/topic/urls.py
,增加第 10 行路徑規則:開啟
forum/topic/views.py
,增加以下視圖類別來處理刪除回覆的請求:DeleteView
的衍生類別裡可透過其get_object()
方法來取得欲刪除的那筆紀錄更新檢視討論主題頁面範本
開啟
forum/pagetmpl/topic/topic_detail.html
,修改為以下程式碼: