Friday, April 17, 2020

Re: Capture URL values in a CBV

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.


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  
--   Tim  tj49.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+unsubscribe@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
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52390ecd-3748-6c96-3a1b-a2d671322336%40akwebsoft.com.
--   Tim  tj49.com

No comments:

Post a Comment