【问题标题】:django render_to_response is not defined errordjango render_to_response 未定义错误
【发布时间】:2011-07-27 04:56:51
【问题描述】:

您好,我在尝试制作一个简单的应用时遇到此错误。

NameError at /first/

global name 'render_to_response' is not defined

Request Method:     GET
Request URL:    http://localhost:8000/first/?preview
Django Version:     1.3
Exception Type:     NameError
Exception Value:    

global name 'render_to_response' is not defined

Exception Location:     /home/naveen/django_projects/myproject/first/views.py in index, line 5
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/naveen/django_projects/myproject',
 '/usr/local/lib/python2.6/dist-packages/pip-0.8.3-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/local/lib/python2.6/dist-packages/django_evolution-0.6.2-py2.6.egg',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/PIL',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/python2.6/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

Server time:    Thu, 31 Mar 2011 14:50:32 -0500

有什么想法吗?!

【问题讨论】:

  • 你能发布一些你的 view.py 代码吗?显而易见的答案是缺少from django.shortcuts import render_to_response

标签: python django-cms render-to-response


【解决方案1】:

from django.shortcuts import render_to_response 已弃用。 使用 django.shortcuts 导入渲染

def renderView(request):
input = "Best Times, render"
return render(request,'mytest/home.html',{'mytime': input})

【讨论】:

    【解决方案2】:

    如果您在 2017 年之后阅读本文,请不要 render_to_response 只是 render

    https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#render-to-response

    Source - Django official documentation for version - 1.11 
    " render_to_response()¶
    
    render_to_response(template_name, context=None, content_type=None, status=None, using=None)[source]¶
    
    This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response. 
    It’s not recommended and is likely to be deprecated in the future."
    

    【讨论】:

      【解决方案3】:

      您是否尝试将 render_to_response 导入为from django.shortcuts import render_to_response

      【讨论】:

      • 已弃用,见下文。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多