Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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/2/django/23.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
Python Django:如何访问其他型号&x27;urlpatterns中的主键是什么?_Python_Django - Fatal编程技术网

Python Django:如何访问其他型号&x27;urlpatterns中的主键是什么?

Python Django:如何访问其他型号&x27;urlpatterns中的主键是什么?,python,django,Python,Django,当路由到特定路径时,我将获得NoReverseMatch 在获取所有其他类似路径的结果时,无法仅为一个urlpattern找到主键 我假设这个错误是因为这里的模型不同 未获取以下项的错误: class PostUpdateView(): model = A 我得到的错误是: class AddCommentView(): model = B urlpatterns=[ 路径('post//update/',PostUpdateView.as_view(),name='post u

当路由到特定路径时,我将获得
NoReverseMatch

在获取所有其他类似路径的结果时,无法仅为一个urlpattern找到主键

我假设这个错误是因为这里的模型不同

未获取以下项的错误:

class PostUpdateView():
   model = A
我得到的错误是:

class AddCommentView():
   model = B
urlpatterns=[
路径('post//update/',PostUpdateView.as_view(),name='post update'),
路径('post//comment',AddCommentView.as_view(),name='post comment')]
这两个类都在同一个views.py文件中,因为我需要在路由url中使用模型A的主键,以便可以反向到原始页面

错误:

Reverse for 'post-comment' with no arguments not found. 1 pattern(s) tried: ['post/(?P<pk>[0-9]+)/comment$']
未找到任何参数的“post comment”的反转。尝试了1个模式:[“post/(?P[0-9]+)/comment$”]
在同一路线中包含两个型号的钥匙的正确方法是什么


注意:A的主键作为外键出现在B中。

您的
get\u absolute\u url
方法应该如下所示
kwargs
参数

from django.urls import reverse

class AddCommentView():
   model = B

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

您没有在URL路由中传递特定的ID,因为您在URL.py
post//comment
中提到这意味着URL反向需要ID来生成URLdynamically@NeerajKumar我试图传递不同的密钥,但得到相同的错误。请给我一些代码,看看您为生成URL做了什么?
from django.urls import reverse

class AddCommentView():
   model = B

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