简体   繁体   English

Django - 使用通用视图

[英]Django - using generic views

I am trying to use Django generic view for CRUD. 我正在尝试使用Django通用视图进行CRUD。

I found two resources ( 1 , 2 ), and bit confused the best and easy approach. 我发现了两个资源( 12 ),并且有点糊涂了最好的和简单的方法。

  1. Added below to myapp/urls.py 以下添加到myapp / urls.py
 urlpatterns = patterns('', url(r'^$', ListView.as_view( model= Product)), ) 

then it gave an error that, 然后它给出了一个错误,

Exception Type: TemplateDoesNotExist
Exception Value:    
myapp/product_list.html

It worked when I created a file product_list.html. 它在我创建文件product_list.html时起作用。 But, do I have to manually write the template? 但是,我是否必须手动编写模板? I am sure not. 我肯定不会。

Also, how to decorate it so that only users of a group has access to it. 此外,如何装饰它,以便只有组的用户才能访问它。

Thanks. 谢谢。

The decorator can be applied inside the urlpatterns like so: 装饰器可以在urlpatterns中应用,如下所示:

urlpatterns = patterns('',
    url(r'^$', my_decorator(ListView.as_view(model= Product))),
)

Yes you have to manually write the template. 是的,您必须手动编写模板。 Also the name of the template is the_model_name_list.html by default but you can also define a custom template name like so: 此外,模板的名称默认为the_model_name_list.html,但您也可以定义自定义模板名称,如下所示:

urlpatterns = patterns('',
    url(r'^$', my_decorator(ListView.as_view(model= Product,
                                             template_name="custom_name.html"))),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM