如何在 django 中创建用于删除记录的删除按钮
Posted
技术标签:
【中文标题】如何在 django 中创建用于删除记录的删除按钮【英文标题】:How to create delete button for deleting a record in django 【发布时间】:2021-04-22 00:27:20 【问题描述】:我正在尝试删除一条记录 我的 views.py 文件是
def AnnouncementDelete(request, pk):
announcement = get_object_or_404(Announcement, pk=pk)
if request.method=='POST':
announcement.delete()
return redirect('/')
return render(request, 'clas-s-room/announcement_confirm_delete.html')
我的html文件是
% extends "clas-s-room/base.html" %
% load crispy_forms_tags %
% block content %
<form action="% url 'clas-s-room:AnnouncementDelete' announcement.id %" method="post">
% csrf_token %
<input type="submit" value="Delete cat">
</form>
% endblock content%
我的 url.py 文件有模式
url(r'^delete/(?P<pk>[0-9]+)/$', views.AnnouncementDelete, name='AnnouncementDelete'),
我要删除记录的模型是
class Announcement(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
def __str__(self):
return self.title
当我尝试访问http://127.0.0.1:8000/clas-s-room/delete/1/时
它给了我以下错误
NoReverseMatch at /clas-s-room/delete/1/
Reverse for 'AnnouncementDelete' with arguments '('',)' not found. 1 pattern(s) tried: ['clas-s-room/delete/(?P<pk>[0-9]+)/$']
我也是 django 的初学者,对url(r'^delete/(?P<pk>[0-9]+)/$', views.AnnouncementDelete, name='AnnouncementDelete'),
方式不是很熟悉。我一般使用```path``方式。
编辑 这是上传作业的视图
@login_required
def upload_announcement(request):
if(request.user.is_teacher==False):
return HttpResponse("This forms requires teacher previlodge")
else:
assignment_uploaded = False
teacher = request.user.Teacher
if request.method== 'POST':
form = AnnouncementForm(request.POST)
if form.is_valid():
upload = form.save(commit=False)
upload.teacher = teacher
upload.save()
assignment_uploaded = True
else:
form = AnnouncementForm()
return render(request, 'clas-s-room/announcement_form.html', 'form':form, 'assignment_uploaded':assignment_uploaded)
这是显示所有作业的视图
class AnnouncementListView(ListView):
context =
'announcements' : Announcement.objects.all()
model = Announcement
template_name = 'clas-s-room/all_announcements.html' #<app>/<model>_<viewtype>.html
context_object_name = 'announcements'
【问题讨论】:
显示您的view
以呈现announcement
html
在你的 urls.py 中尝试path('delete/<int:pk>', ...
。
path('delete/<int:pk>', ...
返回页面未找到错误
@SLDem 编辑有问题
【参考方案1】:
您可以丢弃 url() 行并使用 path() 变体,如下所示:
path("delete/<int:pk>/", view=views.AnnouncementDelete, name="AnnouncementDelete"),
如果这不能解决您的问题,请使用呈现删除页面的视图进行报告。
【讨论】:
我的页面没有错误是因为delete/<int:pk>
与path("delete/<int:pk>/", view=views.AnnouncementDelete, name="AnnouncementDelete"),
一起工作后没有正斜杠【参考方案2】:
在您的 html 模板中,您使用的变量 (announcement
) 永远不会从您的视图中传递出去。
如果我认为这是正确的,你应该改变你的观点:
def AnnouncementDelete(request, pk):
announcement = get_object_or_404(Announcement, pk=pk)
if request.method=='POST':
announcement.delete()
return redirect('/')
return render(request, 'clas-s-room/announcement_confirm_delete.html', 'announcement': announcement)
# this last guy over here sends the desired object to the template for rendering.
你没有要求这个但是......
由于您在问题中提到您大部分时间都使用path
,我假设您只是想测试url
。
关于url
变体的正确使用请查看this answer。
【讨论】:
Alog with page not found 错误我得到The current path, clas-s-room/delete/1/, didn't match any of these.
您是否在项目的 url 中包含了您的应用程序 url?即# project urls urlpatterns = [ path('myapp/', include('myapp.urls')), ]
是的,我做到了。我之前在这个项目中也创建了各种 url以上是关于如何在 django 中创建用于删除记录的删除按钮的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Kendo UI 网格中创建自定义删除/销毁按钮/命令?