Apache web服务器'dict'对象上的Django没有属性'render_context'

Joh*_*acs 6 html python apache django mod-python

我遇到了一些问题,我将我的Django项目上传到运行apache,mod_python和django的网络服务器.在我开发的计算机上,下面的工作很好

nameBox = getNamesBox().render(locals())
Run Code Online (Sandbox Code Playgroud)

-

def getNamesBox():
    users = User.objects.filter()

    templateString = '<select name="name box">'
    for user in users:
        templateString += '<option value="' + user.name + '"> ' + user.name + '</option>'

    templateString += '</select>'

    template = Template(templateString)

    return template
Run Code Online (Sandbox Code Playgroud)

但是在Web服务器上,当从apache或manage.py runserver运行时,它说

AttributeError at /order_site/order/
'dict' object has no attribute 'render_context'
Run Code Online (Sandbox Code Playgroud)

两台机器上的代码是相同的,所以我觉得可能是其他一些问题?它不能渲染我的形式,我不知道为什么.

Raf*_*ler 15

a上的render()方法TemplateContext对象为参数,而不是dict.你必须Context从dict 构造一个对象,例如

namedbox = getNamesBox().render(Context(locals()))
Run Code Online (Sandbox Code Playgroud)

  • 可以使用`from django.template import Context`导入 (4认同)
  • 在较新的版本中`render()`将dict作为没有此方法的上下文 (2认同)