Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将HTML文件从Django视图传递到Django模板_Python_Html_Django - Fatal编程技术网

Python 将HTML文件从Django视图传递到Django模板

Python 将HTML文件从Django视图传递到Django模板,python,html,django,Python,Html,Django,我试图通过Django模板从Django视图传递一个html文件。我的看法是: def sample_function(request, product_id, slug=None): from django.template import loader product_data = get_object_or_404(Product, id=product_id) country_code = request.session.get('country_code') form =

我试图通过Django模板从Django视图传递一个html文件。我的看法是:

def sample_function(request, product_id, slug=None):
  from django.template import loader
  product_data = get_object_or_404(Product, id=product_id)
  country_code = request.session.get('country_code')
  form = CountryForm(initial={'country': country_code})
  form_estimate = CountryEstimateForm(initial={'country_estimate': country_code})


  test = loader.get_template('home/custom_options.html')


  return render(request, 'request_custom_order.html', {'product':product_data,'country': form, 'country_estimate': form_estimate,'test':test})


这是我的html:


<div>


{{ test }}

</div>

但是,如果我只做{{test},我会得到:

如果我做{{test | safe}},我只会得到空白

有人能帮我理解我做错了什么吗?

测试是一个模板对象。您需要以字符串的形式获取其内容

from django.template.loader import render_to_string
...
test = render_to_string('home/custom_options.html')

为什么要传递文件?只需键入要传递到另一个模板中的模板内容。嘿!因为我想根据各种标准动态地传递相当大的html表单。我认为将每个表单保存在自己的文件中会更容易管理。否则,我认为视图文件会很快失去控制。