Hey Tim

The bigger question is, what are you trying to achieve? 

With the DetailView, fetching the object with the primary key is already 
handled for you, as you've seen the object will already be available in the 
template.

This website is very useful to know which functions are implemented in a 
class-based view, and which variables are available.

http://ccbv.co.uk/


On Thursday, April 16, 2020 at 8:26:00 AM UTC+8, tim042849 wrote:
>
> using django.VERSION (2, 1, 5, 'final', 0) with 
>
> python 3.7.2 on ubuntu 16.04 
>
> Given the URL pattern below:
>
> path('<int:pk>', ArticleDetailView.as_view(), name='article_detail'),
>
> And the view as below:
>
> class ArticleDetailView(DetailView):
>     model = Article
>     template_name = 'article_detail.html'
>     login_url = 'login'
>
> I can access the variable pk from a template as 
>
> article.pk
>
> But I don't know how to access the pk variable from the view itself.
>
> Adding the get_queryset method to the view doesn't work
>
> example 
>
> def get_queryset(self):
>         print(self.kwargs["pk"])
>
> results in 
>
> 'NoneType' object has no attribute 'filter'
>
> Trying 
>     def get_object(self):
>         queryset = self.filter_queryset(self.get_queryset())
>         obj = queryset.get(pk=self.kwargs['pk'])
>         return obj
> results in
>   'ArticleDetailView' object has no attribute 'filter_queryset'
>
> Please advise - pretty basic for django, new to me :)
> thanks
>
> -- 
> Timtj49.com
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/39595843-0a01-4ffb-95b9-46d2d3c442e3%40googlegroups.com.

Reply via email to