Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django NoReverseMatch at/更新/_Django_Django Forms_Django Templates - Fatal编程技术网

Django NoReverseMatch at/更新/

Django NoReverseMatch at/更新/,django,django-forms,django-templates,Django,Django Forms,Django Templates,我在url中使用作者姓名发布文章。在更新帖子或创建新帖子时,单击“提交”按钮后出现此错误: Reverse for 'post-detail' with keyword arguments '{'id': 2, 'author': 'john'}' not found. 1 pattern(s) tried: ['(?P<author>[^/]+)\\/(?P<pk>[0-9]+)\\/$'] Views.py class PostUpdateView(LoginReq

我在url中使用作者姓名发布文章。在更新帖子或创建新帖子时,单击“提交”按钮后出现此错误:

Reverse for 'post-detail' with keyword arguments '{'id': 2, 'author': 'john'}' not found. 1 pattern(s) tried: ['(?P<author>[^/]+)\\/(?P<pk>[0-9]+)\\/$']
Views.py

class PostUpdateView(LoginRequiredMixin, UpdateView):
    model = Post 
    fields = ['title', 'content']

    def form_valid(self, form):
        form.instance.author = self.request.user ##author = current logged in user
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True 
        return False 
url.py

urlpatterns = [

    
    path('<str:author>/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('<str:author>/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
    path('<str:author>/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
   

]
urlpatterns=[
路径('/',PostDetailView.as_view(),name='post-detail'),
路径('//update//',PostUpdateView.as_view(),name='post-update'),
路径('//delete//',PostDeleteView.as_view(),name='post-delete'),
]

URL接受两个关键字参数,分别是'pk''author',因此您需要将get\u绝对URL更改为

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

不工作,出现此错误:找不到具有关键字参数“{pk':1}”的“post detail”的反转。尝试了1个模式:['(?P[^/]+)\\\/(?P[0-9]+)\\/$”]对不起,我的错误,请重新检查我的回答您需要共享PostDetailView,因为作者参数需要是字符串,您可能需要从self.auhor更改为self.author.username或self.author.email
def get_absolute_url(self):
    return reverse('post-detail', kwargs={'pk': self.pk, 'author': self.author})