I am sure I have made a rookie mistake somewhere down here. So I have a details page for a particular link. Suppose I have list of incubators on my page, and if I click on one of them, I want to show its details. I am sure this can be done by using primary key but I keep getting errors.
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})
This is my urls.py but I'm sure there's no error here:
url(r'^incubators/(?P<pk>[0-9]+)', views.details, name = 'details'),
Can you explain a little why I am getting this error?
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'
In your URL patterns, you're calling the variable "pk" but in your view you're calling it incubator_id
To fix this, change your URL pattern from:
url(r'^incubators/(?P<pk>[0-9]+)', views.details, name = 'details'),
to
url(r'^incubators/(?P<incubator_id>[0-9]+)', views.details, name = 'details'),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.