我确信我在这里的某个地方犯了一个菜鸟错误。 所以我有一个特定链接的详细信息页面。假设我的页面上有孵化器列表,如果我点击其中一个,我想显示其详细信息。我确信这可以通过使用主键来完成,但我不断收到错误。
models.py
class Incubators(models.Model):
# I have made all the required imports
incubator_name = models.CharField(max_length=30)
owner = models.CharField(max_length=30)
city_location = models.CharField(max_length=30)
description = models.TextField(max_length=100)
logo = models.FileField()
verify = models.BooleanField(default = False)
def get_absolute_url(self):
return reverse('main:details', kwargs={'pk': self.pk})
def __str__(self): # Displays the following stuff when a query is made
return self.incubator_name + '-' + self.owner
class Details(models.Model):
incubator = models.ForeignKey(Incubators, on_delete = models.CASCADE)
inc_name = models.CharField(max_length = 30)
inc_img = models.FileField()
inc_details = models.TextField(max_length= 2500)
inc_address = models.TextField(max_length = 600, default = "Address")
inc_doc = models.FileField()
inc_policy = models.FileField()
def __str__(self):
return self.inc_name
views.py
def details(request, incubator_id):
inc = get_object_or_404(Incubators, pk = incubator_id)
return render(request, 'main/details.html', {'inc': inc})
这是我的urls.py,但我确定此处没有错误:
url(r'^incubators/(?P<pk>[0-9]+)', views.details, name = 'details'),
你能解释一下为什么我会收到这个错误吗?
TypeError at /incubators/9
details() got an unexpected keyword argument 'pk'
Request Method: GET
Request URL: http://127.0.0.1:8000/incubators/9
Django Version: 1.11.3
Exception Type: TypeError
Exception Value:
details() got an unexpected keyword argument 'pk'
答案 0 :(得分:2)
在您的网址格式中,您需要调用变量&#34; pk&#34;但在您看来,您将其称为incubator_id
要解决此问题,请从以下位置更改您的网址格式:
url(r'^incubators/(?P<pk>[0-9]+)', views.details, name = 'details'),
到
url(r'^incubators/(?P<incubator_id>[0-9]+)', views.details, name = 'details'),