python - How can i use custom function in class based views -


i have view

class userview(genericapiview):     def get(self, request, format=none, **kwargs):         pass      def post(self, request, format=none, **kwargs):         pass 

this works fine url

url(r'^user$', userview.as_view(),name='user'), 

but want have custom url

def  custom():       pass 

i want

url(r'^user/custom/$', userview.as_view(custom),name='user'), 

how can that

you can't this.

from django.conf.urls import url django.views.generic import templateview  urlpatterns = [     url(r'^about/', templateview.as_view(template_name="about.html")), ] 

any arguments passed as_view() override attributes set on class. in example, set template_name on templateview. similar overriding pattern can used url attribute on redirectview.

if want 'custom' url, use functions based views

urls

url(r'^user/custom/$', custom, name='user'), 

views

def custom(request):      # custom logic      # return 

edit 1* if want pass parameters cbv.

class view(detailview):     template_name = 'template.html'     model = mymodel     # custom parameters     custom = none      def get_object(self, queryset=none):         return queryset.get(custom=self.custom) 

url

url(r'^about/', myview.as_view(custom='custom_param')), 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -