Django 基于类的视图覆盖泛型视图方法
我试着让它工作:Django 基于类的视图覆盖泛型视图方法,django,django-class-based-views,Django,Django Class Based Views,我试着让它工作: url(r'^index$', Index.as_view(), name='index'), url(r'^index/(?P<pk>[0-9]+)$', Index.as_view(), name='index'), 源自 class MyViewBase(TemplateView, DetectMobileMixin, TemplateResponseMixin, ContextMixin): 默认情况下,它是渲染模板,但我为Index class jso
url(r'^index$', Index.as_view(), name='index'),
url(r'^index/(?P<pk>[0-9]+)$', Index.as_view(), name='index'),
源自
class MyViewBase(TemplateView, DetectMobileMixin, TemplateResponseMixin, ContextMixin):
默认情况下,它是渲染模板,但我为Index
class jsonMixin(object):
def get(self, *args, **kwargs):
if 'pk' in kwargs:
if self.request.is_ajax():
pk = kwargs['pk']
data = self.data_json(pk)
json_data = json.dumps(data)
return HttpResponse(json_data,content_type='application/json')
else:
self.context["pokaz_element"] = self.id
return super().get(*args, **kwargs)
class Index(MyViewBase, jsonMixin):
...
def data_json():
data = []
...
return data
但它似乎不起作用。
我不知道怎么咬它
我试图理解基于类的视图是如何工作的,从中派生出什么,调用哪个函数get
等等,但手册没有对此进行解释
我真的想在一个类中实现这一点我认为应该将mixin放在MyViewBase之前,因为Python方法是查找的
class Index(jsonMixin, MyViewBase):
...
def data_json():
data = []
...
return data
希望有助于类索引(jsonMixin,MyViewBase)
??
class Index(jsonMixin, MyViewBase):
...
def data_json():
data = []
...
return data