Django:在基于类的视图上使用render_to_string()
我正在使用DetailView的一个复杂子类来呈现我的模板Django:在基于类的视图上使用render_to_string(),django,django-views,Django,Django Views,我正在使用DetailView的一个复杂子类来呈现我的模板 class Test(DetailView): template_name = 'my_template.html' model = MyModel def ..my_methods.. (self, ...): ... return result def get_context_data(self, **kwargs): context = supe
class Test(DetailView):
template_name = 'my_template.html'
model = MyModel
def ..my_methods.. (self, ...):
...
return result
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
return context
如何使用此视图,以便能够选择将结果呈现为字符串并将其保存在变量中
我可以想到render\u to\u string()
,但我不确定如何将其用于基于类的视图
编辑
它还可以校准
dispatch()
方法,以便仅在应用给定关键字时呈现到字符串()
。呈现到字符串
需要这些参数模板名称
,字典
,context\u实例
其中dictionary和context\u实例的默认值为None。根据它的定义
加载给定的模板名称并使用给定的字典呈现它
作为上下文。模板名称可以是用于加载单个模板的字符串
使用get_模板创建模板,或者它可能是要使用的元组
选择_template可在列表中查找其中一个模板。返回一个
绳子
只需导入函数:
from django.template.loader import render_to_string
在继承自DetailView
的类的get
函数中,只需在给定的定义格式中使用它
class Test(DetailView):
def get(self, request, *args, **kwargs):
return render_to_string(<template_name>, <dictionary>)
类测试(详细视图):
def get(自我、请求、*args、**kwargs):
将渲染返回到字符串(,)
字典作为上下文数据传递给模板。
呈现到字符串
需要这些参数模板名称
,字典
,上下文实例
,其中字典和上下文实例的默认值为无。根据它的定义
class Test(DetailView):
template_name = 'my_template.html'
model = MyModel
def ..my_methods.. (self, ...):
...
return result
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
return context
加载给定的模板名称并使用给定的字典呈现它
作为上下文。模板名称可以是用于加载单个模板的字符串
使用get_模板创建模板,或者它可能是要使用的元组
选择_template可在列表中查找其中一个模板。返回一个
绳子
只需导入函数:
from django.template.loader import render_to_string
在继承自DetailView
的类的get
函数中,只需在给定的定义格式中使用它
class Test(DetailView):
def get(self, request, *args, **kwargs):
return render_to_string(<template_name>, <dictionary>)
类测试(详细视图):
def get(自我、请求、*args、**kwargs):
将渲染返回到字符串(,)
字典作为上下文数据传递给模板。
呈现到字符串
需要这些参数模板名称
,字典
,上下文实例
,其中字典和上下文实例的默认值为无。根据它的定义
class Test(DetailView):
template_name = 'my_template.html'
model = MyModel
def ..my_methods.. (self, ...):
...
return result
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
return context
加载给定的模板名称并使用给定的字典呈现它
作为上下文。模板名称可以是用于加载单个模板的字符串
使用get_模板创建模板,或者它可能是要使用的元组
选择_template可在列表中查找其中一个模板。返回一个
绳子
只需导入函数:
from django.template.loader import render_to_string
在继承自DetailView
的类的get
函数中,只需在给定的定义格式中使用它
class Test(DetailView):
def get(self, request, *args, **kwargs):
return render_to_string(<template_name>, <dictionary>)
类测试(详细视图):
def get(自我、请求、*args、**kwargs):
将渲染返回到字符串(,)
字典作为上下文数据传递给模板。
呈现到字符串
需要这些参数模板名称
,字典
,上下文实例
,其中字典和上下文实例的默认值为无。根据它的定义
class Test(DetailView):
template_name = 'my_template.html'
model = MyModel
def ..my_methods.. (self, ...):
...
return result
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
return context
加载给定的模板名称并使用给定的字典呈现它
作为上下文。模板名称可以是用于加载单个模板的字符串
使用get_模板创建模板,或者它可能是要使用的元组
选择_template可在列表中查找其中一个模板。返回一个
绳子
只需导入函数:
from django.template.loader import render_to_string
在继承自DetailView
的类的get
函数中,只需在给定的定义格式中使用它
class Test(DetailView):
def get(self, request, *args, **kwargs):
return render_to_string(<template_name>, <dictionary>)
类测试(详细视图):
def get(自我、请求、*args、**kwargs):
将渲染返回到字符串(,)
字典作为上下文数据传递给模板。django.template.loader import render_to_string中的包名
拼写有点错误
django.template.loader import render_to_string中的包名拼写有点错误
django.template.loader importrender_to_string只是django.template.loader中的包名有点拼写错误,请导入render_to_string
class Test(DetailView):
template_name = 'my_template.html'
model = MyModel
def ..my_methods.. (self, ...):
...
return result
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
return context