Django在Apache服务器上'字典'对象没有'render_context'属性

7 投票
1 回答
8967 浏览
提问于 2025-04-16 19:29

我遇到了一点问题,我把我的Django项目上传到了一个运行着apache、mod_python和django的网络服务器上。在我开发的电脑上,以下代码运行得很好

nameBox = getNamesBox().render(locals())

-

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

但是在网络服务器上,当我从apache或者用manage.py runserver运行时,它显示

AttributeError at /order_site/order/
'dict' object has no attribute 'render_context'

这两台机器上的代码是完全一样的,所以我觉得可能是其他问题?它无法渲染我的表单,我也不知道为什么。

1 个回答

17

在一个Template中,render()这个方法需要一个Context对象作为参数,而不是一个字典(dict)。所以你需要从字典中创建一个Context对象,比如:

namedbox = getNamesBox().render(Context(locals()))

撰写回答