简体   繁体   English

Django的嵌套render_to_response始终显示内容类型

[英]Django's nested render_to_response always show content-type

Currently I am trying to create multiple view for one request. 目前,我正在尝试为一个请求创建多个视图。 For 1 view, I am doing this: 对于1个视图,我正在这样做:

return render_to_response(
    "/index/index.html",
    {}
)

And now when I try to add a "left" column to index.html, I need to put it on different view (because I need to apply the same technique on other place as well), this is how I do it: 现在,当我尝试向index.html添加“左”列时,我需要将其放在不同的视图上(因为我也需要在其他地方应用相同的技术),这就是我的方法:

leftCol = direct_to_template(request,settings.viewPath + "/columns/left.html",{})
return render_to_response(
    "/index/index.html",
    {
    'leftColumn': leftCol,
    }

The code works well, but the output is not what I expected. 该代码运行良好,但是输出不是我期望的。 The leftCol shows the response header at the beginning of it's output: leftCol在输出的开头显示响应标头:

"Content-Type: text/html; charset=utf-8" “内容类型:text / html; charset = utf-8”

How do I remove this header? 如何删除此标头? I've been trying to modify the content_type and mimetype in the parameter but it didn't work. 我一直在尝试修改参数中的content_type和mimetype,但是没有用。

That's because direct_to_template() returns a HttpResponse , not a string. 这是因为direct_to_template()返回HttpResponse而不是字符串。 Have you considered using templating functionality, eg the {% include %} template tag, or writing a template tag of your own? 您是否考虑过使用模板功能,例如{% include %}模板标签,或编写自己的模板标签?

If you insist on pre-rendering templates in your view and then combining them in your template, render the template yourself, rather than using direct_to_template() . 如果您坚持在视图中预渲染模板,然后将其组合到模板中,请自己渲染模板,而不要使用direct_to_template() eg 例如

from django.template.loader import get_template
from django.template import RequestContext

def someview(request):
    leftCol = get_template(settings.viewPath + "/columns/left.html").render(RequestContext(request)
    render_to_response("/index/index.html", {'leftColumn': leftCol})

Use render_to_string ( http://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut ) to get a string back after rendering a template. 渲染模板后,请使用render_to_stringhttp://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut )来获取字符串。 Alternatively, you can use an {% include %} to include a template in the same context as the current template (but this is still manual). 另外,您可以使用{% include %}在与当前模板相同的上下文中包含模板(但这仍然是手动的)。 Even better would be to have a base template that you inherit using {% extends 'base.html' %} which will just include common template functionality that you can override at your will using {% block %} and enables you to leave out duplicated template content like a left column. 更好的办法是使用{% extends 'base.html' %}继承一个基础模板,该{% extends 'base.html' %}将仅包含通用模板功能,您可以使用{% block %}随意重写这些模板功能,并避免重复模板内容,如左列。

The 'render_to_response' function returns an HttpResponse object. “ render_to_response”函数返回HttpResponse对象。 Instead of returning the object itself, you can return its content attribute to access only the output you want to render. 您可以返回其内容属性以仅访问要渲染的输出,而不是返回对象本身。

ie

response = render_to_response(
    "/index/index.html",
    {
    'leftColumn': leftCol,
    }
return response.content

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

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