1👍
On class based views:
self.kwargs['pk']
So, in your case
class PetProfileView(DetailView):
model = Pet
template_name = 'pet_profile.html'
def get_context_data(self, **kwargs):
context['key'] = self.get_object().birth_date
return context
I don’t know what you want to do with the "key" variable. This example is to use on your template. You can access to "birth_date" writting {{key}}
or {{object.birth_date}}
0👍
As already mentioned you can access the URL parameters like self.kwargs["pk"]
. But as you are already using a DetailView
which already provides the functionality to retrieve an object via its id you can customize the queryset it uses:
class PetProfileView(DetailView):
model = Pet
template_name = 'pet_profile.html'
queryset = Pet.objects.values_list('birth_date')
(Though without doing this you should also be able to access pet.birth_date
in your template)
- [Answered ]-Django – place differently every second item in for loop
- [Answered ]-Recaptcha Django cms – captcha validation take more than a minute
- [Answered ]-Django Rest Framework, two different approach to POST request
Source:stackexchange.com