Handling url in class based views.

36 views
Skip to first unread message

Aadil Rashid

unread,
Nov 15, 2021, 4:28:12 PM11/15/21
to django...@googlegroups.com
Hello my dear Friends, I have a question regarding Class Based Views. 

If we have a url e.g, 
path('item/<str:slug>/',  views.funView) 

We can handle it in  Function Based Views,
By simply
def funView(request, slug) :
       #logic
       return render(request, "tempName") 


We can even pass this slug as a context in this template, 


My question is how can we achieve this by using class Based Views. 


Thanks in advance. 

pankaj palmate

unread,
Nov 15, 2021, 11:11:13 PM11/15/21
to django...@googlegroups.com
def get(self, pk, *args,**kwargs ) :
        .......body..........     

--
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/CAAYXZx_Sn2BBhPWZXi_zEECN-x7wu9sDQJfYeVH31oHhemptpQ%40mail.gmail.com.

pankaj palmate

unread,
Nov 15, 2021, 11:11:55 PM11/15/21
to django...@googlegroups.com
Also request in def get itself 

Aadil Rashid

unread,
Nov 16, 2021, 12:11:32 AM11/16/21
to django...@googlegroups.com

Thank you very much Pankaj Palmate, I really appreciate it.
I have tried using def get(self, request, *args, **kwargs) but This method is not been invoked, 
Actually the thing is that, 
I am calling this ClassBasedView by an anchor tag, which passes URL to this link and I need to get the information in which is present in the URL,
I am confused, weather the request initiated by an anchor tag is a get request or not, 
Indeed get method does not worked here,

I once again thank you very much 

pankaj palmate


Perry Bates

unread,
Nov 19, 2021, 10:12:08 AM11/19/21
to django...@googlegroups.com
You can use the DetailListView from generic views.
Pass in the template name, form class, and query(which in this case takes in the slug to query the db)


Lalit Suthar

unread,
Nov 21, 2021, 1:43:01 AM11/21/21
to django...@googlegroups.com
you can get that slug value from kwargs

def get(self, request, *args, **kwargs):
        slug = self.kwargs.get("slug")

also if you like to pass it in the context you can override get_context_data

refer: https://ccbv.co.uk/
to know more about class based views you can refer to this playlist from video number 36

Reply all
Reply to author
Forward
0 new messages