Handling url in class based views.

瀏覽次數:36 次
跳到第一則未讀訊息

Aadil Rashid

未讀,
2021年11月15日 下午4:28:122021/11/15
收件者: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

未讀,
2021年11月15日 晚上11:11:132021/11/15
收件者: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

未讀,
2021年11月15日 晚上11:11:552021/11/15
收件者:django...@googlegroups.com
Also request in def get itself 

Aadil Rashid

未讀,
2021年11月16日 凌晨12:11:322021/11/16
收件者: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

未讀,
2021年11月19日 上午10:12:082021/11/19
收件者: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

未讀,
2021年11月21日 凌晨1:43:012021/11/21
收件者: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

回覆所有人
回覆作者
轉寄
0 則新訊息