[英]Convert the Django direct_to_template function to use class based view
I am updating a Django project which used the direct_to_template as a function ie: 我正在更新一个使用direct_to_template作为功能的Django项目,即:
return direct_to_template(request, 'bprofile/init.html', targs)
As described a short way down this page 如本页简短描述
I have seen the SO question here and read the documentation on this page which decribe the migration of statements of the form 我在这里看到了SO问题,并阅读了此页面上的文档,该文档描述了表单语句的迁移
('^about/$', direct_to_template, {'template': 'about.html'})
to look like 看起来像
('^about/$', TemplateView.as_view(template_name='about.html'))
Unfortunatelty I cannot seem to figure out how to change statements from the form that I have into a working new form. 不幸的是,我似乎无法弄清楚如何将陈述式从已有的表格更改为可以使用的新表格。
How might one modify this code to work with the new Templateview form? 如何修改此代码以使用新的Templateview表单?
You shouldn't be using the generic views for this, that's not what they are for. 您不应该为此使用通用视图,这不是它们的用途。 If you want to render a template, you should use the render
shortcut : it takes exactly the same arguments. 如果要渲染模板,则应使用render
快捷方式 :它采用完全相同的参数。
return render(request, 'bprofile/init.html', targs)
To use TemplateView you import TemplateView into your urls.py: 要使用TemplateView,请将TemplateView导入urls.py:
from django.views.generic.base import TemplateView
Then you just add the urlconf: 然后,您只需添加urlconf:
('^about/$', TemplateView.as_view(template_name='bprofile/init.html'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.