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 }}
See
https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-display/#adding-extra-context

Dylan

On Fri, Apr 17, 2020 at 1:49 PM Tim Johnson <[email protected]> wrote:

> 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
> On 4/16/20 7:26 AM, Tim Johnson wrote:
>
>
> 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.
>
> http://ccbv.co.uk/
>
> Thanks for the link. Will study it after finishing my coffee.
>
>
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/39595843-0a01-4ffb-95b9-46d2d3c442e3%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/39595843-0a01-4ffb-95b9-46d2d3c442e3%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/52390ecd-3748-6c96-3a1b-a2d671322336%40akwebsoft.com
> <https://groups.google.com/d/msgid/django-users/52390ecd-3748-6c96-3a1b-a2d671322336%40akwebsoft.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a365875b-a4a1-2214-8616-c2b749f2ef52%40akwebsoft.com
> <https://groups.google.com/d/msgid/django-users/a365875b-a4a1-2214-8616-c2b749f2ef52%40akwebsoft.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHtg44DzV3hq_Wk%2BRmE54qTs%3Dd7pUzKCh_vTkr8UyAQ4zG3PLw%40mail.gmail.com.

Reply via email to