gpt4 book ai didi

python - 如何使用 django 泛型类删除我的帖子?

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:31 25 4
gpt4 key购买 nike

我想删除我的博客文章,但当我单击“确认删除”时,它显示

NoReverseMatch at /post/10/delete/
Reverse for 'post-by-author' with no arguments not found. 1 pattern(s) tried: ['post/detail/(?P<pk>[0-9]+)$']
Request Method: POST
Request URL: http://127.0.0.1:8500/post/10/delete/
Django Version: 3.0.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post-by-author' with no arguments not found. 1 pattern(s) tried: ['post/detail/(?P<pk>[0-9]+)$']
Exception Location: /Users/antonia/PycharmProjects/MySite/venv/lib/python3.8/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 677
Python Executable: /Users/antonia/PycharmProjects/MySite/venv/bin/python
Python Version: 3.8.1
Python Path:
['/Users/antonia/PycharmProjects/MySite/mysite',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/antonia/PycharmProjects/MySite/venv/lib/python3.8/site-packages',
'/Users/antonia/PycharmProjects/MySite/venv/lib/python3.8/site-packages/setuptools-40.8.0-py3.8.egg',
'/Users/antonia/PycharmProjects/MySite/venv/lib/python3.8/site-packages/pip-19.0.3-py3.8.egg']
Server time: Tue, 14 Jan 2020 21:25:06 +0000

删除帖子后我想返回用户的帖子页面吗?我还为 Html 表单 post_confirm_delete 正确命名了该文件。

我试图将reverse_lazy中的内容更改为(posts/post/blogs/blog/blog:index/blog:post-detail/blog:post-by-author)似乎没有任何作用?

class PostDelete(DeleteView):
model = Post
success_url = reverse_lazy('post-by-author')

这是表格

{% extends "../base.html" %}

{% block content %}
<div class="container">
<h1>Delete Post</h1>

<p>Are you sure you want to delete this post: {{ post }}?</p>

<form action="" method="POST">
{% csrf_token %}
<input type="submit" action="" value="Yes, delete." />
</form>
</div>
{% endblock %}

我的模型

class PostAuthor(models.Model):
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, related_name='authors')
bio = models.TextField(max_length=400, help_text="Enter your bio details here.")

class Meta:
ordering = ["user", "bio"]

def get_absolute_url(self):
return reverse('post-by-author', args=[str(self.id)])

def __str__(self):
return self.user.username


class Post(models.Model):
title = models.CharField(max_length=200, unique=False)
slug = models.SlugField(max_length=200, null=True, blank=True)
author = models.ForeignKey(PostAuthor, on_delete=models.CASCADE, null=False, blank=True)
updated_on = models.DateTimeField(auto_now=True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)

class Meta:
ordering = ['-created_on']

def get_absolute_url(self):
return reverse('post-detail', args=[str(self.id)])

def __str__(self):
return self.title

URL 文件

urlpatterns = [
path('', views.IndexPage.as_view(), name='index'),
path('posts/', views.PostList.as_view(), name='all-posts'),
path('post/detail/<int:pk>', views.PostListbyAuthorView.as_view(), name='post-by-author'),
path('post/author/<int:pk>', views.PostDetail.as_view(), name='post-detail'),
path('post/create/', views.PostCreate.as_view(), name='post-create'),
path('post/<int:pk>/edit/', views.PostUpdate.as_view(), name='post-edit'),
path('post/<int:pk>/delete/', views.PostDelete.as_view(), name='post-delete'),
path('post/<int:pk>/comment/', views.PostComment.as_view(), name='post-comment'),
path('accounts/', include('django.contrib.auth.urls')),
]

最佳答案

Django 在您的模型中期待 kwargs,而不是 args:

class PostAuthor(models.Model):

...

def get_absolute_url(self):
return reverse('post-by-author', kwargs={'pk':self.pk})


class Post(models.Model):

...

def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk':self.pk})

这里是docs对于这个主题。

关于python - 如何使用 django 泛型类删除我的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com