python - Django:Internet Explorer 下载页面内容而不是渲染它

标签 python html django internet-explorer

我正在使用 Django Web 框架创建一个网站。 当我使用 chrome、safari、firefox 等浏览器查看它时,它可以正常工作,但如果我在 Internet Explorer 中打开页面,就会发生这种情况:

enter image description here

在我的views.py 中,我有以下代码:

def index(request):

    context = RequestContext(request)

    c_form = False
    try:
        c_form = request.COOKIES['cform']
    except Exception:
    if request.POST: c_form = foo_email(request)

    context_list = {'form': c_form}
    response = render(request, 'base_home.html', context_list, context)
    if c_form: response.set_cookie('cform', value='1', max_age=None)
    return response

response变量包含页面的HTML结构,其他浏览器可以渲染它,但是IE不会,为什么?

提前致谢

最佳答案

您正在向 render() 传递无效参数。来自 docs ,它需要以下参数:

render(request, template_name, context=None, content_type=None, status=None, using=None)

您将 context 传递给 content_type 参数,这在某种程度上不会完全破坏,但最终导致响应不包含 text/html 内容类型。因此 IE 会尝试下载它。

删除最后一个参数:

response = render(request, 'base_home.html', context_list)

关于python - Django:Internet Explorer 下载页面内容而不是渲染它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47069717/

相关文章:

android - 不同 Angular 表现同一张图片

javascript - 使用 JavaScript 的 Google map 无法在 jQuery 选项卡中正确显示

javascript - IE 不记得后退按钮上的隐藏字段值

django - 无法导入模块 'handler' : No module named 'werkzeug'

Python - 零阶保持插值(最近邻)

python - 安装 Flask 时出错 - Python

python - 站点包中模板中的 URL

python - "else"在 Python 中被认为是有害的?

django - 当大请求正文和过期 session 时,Nginx+uWSGI+Django 返回 502

javascript - 在 django 模板中打开链接内容