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
Post a Comment