parent
77b4835a47
commit
6639ec668c
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,46 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.shortcuts import redirect, get_object_or_404
|
||||
from django.views.generic.edit import DeleteView
|
||||
from django.urls import reverse_lazy
|
||||
from .models import Article, Category, Tag, ArticleImage
|
||||
|
||||
|
||||
class ArticleDeleteView(LoginRequiredMixin, DeleteView):
|
||||
"""删除文章视图"""
|
||||
model = Article
|
||||
template_name = 'blog/article_delete_confirm.html'
|
||||
pk_url_kwarg = 'article_id'
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
obj = self.get_object()
|
||||
# 只有文章作者或管理员可以删除
|
||||
if obj.author != request.user and not request.user.is_superuser:
|
||||
return redirect('blog:detailbyid',
|
||||
article_id=obj.id,
|
||||
year=obj.creation_time.year,
|
||||
month=obj.creation_time.month,
|
||||
day=obj.creation_time.day)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['all_categories'] = Category.objects.all()
|
||||
context['all_tags'] = Tag.objects.all()
|
||||
return context
|
||||
|
||||
def delete(self, request, *args, **kwargs):
|
||||
# 删除文章前先删除所有相关图片
|
||||
self.object = self.get_object()
|
||||
images = ArticleImage.objects.filter(article=self.object)
|
||||
for image in images:
|
||||
# 删除图片文件
|
||||
image.image.delete()
|
||||
# 删除图片记录
|
||||
image.delete()
|
||||
|
||||
# 调用父类的delete方法删除文章
|
||||
return super().delete(request, *args, *kwargs)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse_lazy('blog:index')
|
||||
@ -0,0 +1,24 @@
|
||||
# Generated by Django
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('blog', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='articleimage',
|
||||
name='article',
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=models.deletion.CASCADE,
|
||||
related_name='images',
|
||||
to='blog.article'
|
||||
),
|
||||
),
|
||||
]
|
||||
@ -0,0 +1,14 @@
|
||||
# Generated by Django 4.2.14 on 2025-11-20 22:28
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('blog', '0002_auto_20231201_1200'),
|
||||
('blog', '0007_articleimage'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 185 KiB |
|
After Width: | Height: | Size: 324 KiB |
|
After Width: | Height: | Size: 584 KiB |
|
After Width: | Height: | Size: 1.3 MiB |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,56 @@
|
||||
{% extends "share_layout/base.html" %}
|
||||
{% load static %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block title %}{% trans "Delete Article" %}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="card">
|
||||
<div class="card-header bg-danger text-white">
|
||||
<h2>{% trans "Delete Article" %}: {{ article.title }}</h2>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="alert alert-warning">
|
||||
<h4>{% trans "Warning" %}</h4>
|
||||
<p>{% trans "You are about to delete this article. This action cannot be undone." %}</p>
|
||||
<p>{% trans "All associated images will also be deleted." %}</p>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<h5>{% trans "Article Details" %}</h5>
|
||||
<ul>
|
||||
<li><strong>{% trans "Title" %}:</strong> {{ article.title }}</li>
|
||||
<li><strong>{% trans "Created" %}:</strong> {{ article.creation_time|date:"Y-m-d H:i" }}</li>
|
||||
<li><strong>{% trans "Modified" %}:</strong> {{ article.last_modify_time|date:"Y-m-d H:i" }}</li>
|
||||
<li><strong>{% trans "Category" %}:</strong> {{ article.category.name }}</li>
|
||||
<li><strong>{% trans "Tags" %}:</strong>
|
||||
{% for tag in article.tags.all %}
|
||||
{{ tag.name }}{% if not forloop.last %}, {% endif %}
|
||||
{% empty %}
|
||||
{% trans "None" %}
|
||||
{% endfor %}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="confirm" value="true">
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-danger">
|
||||
<i class="fas fa-trash"></i> {% trans "Confirm Delete" %}
|
||||
</button>
|
||||
<a href="{{ article.get_absolute_url }}" class="btn btn-secondary">
|
||||
<i class="fas fa-times"></i> {% trans "Cancel" %}
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Loading…
Reference in new issue