Capture URL values in a CBV

79 views
Skip to first unread message

Tim Johnson

unread,
Apr 15, 2020, 8:26:00 PM4/15/20
to MLDjango

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
-- 
Tim
tj49.com

Gavin Wiener

unread,
Apr 16, 2020, 8:01:07 AM4/16/20
to Django users
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.

Tim Johnson

unread,
Apr 16, 2020, 9:57:51 PM4/16/20
to django...@googlegroups.com


On 4/15/20 7:24 PM, Gavin Wiener wrote:
Hey Tim
Hello Gavin: Thank you for your prompt reply.


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

I gotta know. I'm a retired programmer with 19 years doing CGI. Wrote and implemented my own framework. First in C, than C++ then rebol, then python.

I really want to know what is going on "under the hood" but I also wish to know why those examples, taken from documentation do not work.

I'm building a website with django as an alternative to the original in Drupal because I want fine-grained control.

I hope that answers your question.


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.


Thanks for the link. Will study it after finishing my coffee.
--
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...@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.
-- 
Tim
tj49.com

Tim Johnson

unread,
Apr 17, 2020, 4:50:24 PM4/17/20
to django...@googlegroups.com

OK. I am abandoning all pretext at self-sufficiency here. I have researched this for days, reviewed the links below and I still cannot resolve this issue.

Maybe someone besides Gavin has an idea of how to capture the pattern between angle brackets in a url conf into a class-based view.

I am sure the solution is very easy, but I obviously am missing something.

thanks

Dylan Reinhold

unread,
Apr 17, 2020, 5:09:53 PM4/17/20
to django...@googlegroups.com
Tim you want to use get_context_data to add more data into your context for the template

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # You your stuff
        context['my_pk'] = self.kwargs['pk']
        return context


Then in your template you get get it as {{ my_pk }}

Dylan

Tim Johnson

unread,
Apr 17, 2020, 5:42:27 PM4/17/20
to django...@googlegroups.com
Wham! That is what I was looking for.

From a class-based-view virgin to a wise head:

**you have been so helpful**

thank you

On 4/17/20 1:08 PM, Dylan Reinhold wrote:
> def get_context_data(self, **kwargs):
>         # Call the base implementation first to get a context
>         context = super().get_context_data(**kwargs)
>         # You your stuff
>         context['my_pk'] = self.kwargs['pk']
>         return context

--
Tim
tj49.com

Reply all
Reply to author
Forward
0 new messages