简体   繁体   English

转换Django direct_to_template函数以使用基于类的视图

[英]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.

相关问题 动态direct_to_template - Dynamic direct_to_template 将函数视图转换为基于类的视图 Django - Convert a function view to a Class Based View Django Django - render()、render_to_response() 和 direct_to_template() 之间有什么区别? - Django - what is the difference between render(), render_to_response() and direct_to_template()? 将基于函数的视图转换为基于类的视图 django - convert function based view to class based view django 在django 1.9中相当于django.views.generic.simple import direct_to_template - What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9 如何在Django中将基于函数的视图转换为基于类的视图 - How to convert a Function based view into a Class based View in django 如何在direct_to_template或urls.py中的简单视图中提供会话数据(不创建应用) - How to provide session data in direct_to_template or simple view in urls.py (without creating apps) 我可以在基于 Django 类的视图中使用 function 来确定模板名称吗? - Can I use a function in a Django class-based view to determine template_name? 基于Django函数的视图到基于类的视图 - Django function based view to class based view 如何满足 direct_to_template 的导入? - How can I satisfy an import of direct_to_template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM