2011-06-13 23 views
6

我有點問題,我把我的Django項目上傳到運行apache,mod_python和django的web服務器。在電腦上我精Django在Apache Web服務器上的'dict'對象沒有屬性'render_context'

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 

但在Web服務器上,從Apache或manage.py runserver命令運行時,它說

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

兩臺機器上的代碼都是相同的,所以我覺得可能還有其他一些問題?它無法渲染我的表單,我不知道爲什麼。

+0

你似乎錯過了模板的全部點,那裏。爲什麼使用concatentation手動創建文本,然後「渲染」不包含模板語法的內容,而不是實際使用帶有適當模板邏輯的模板文件,可以爲您完成所有這些工作? – 2011-06-13 16:37:58

+0

或者,更好的是,使用表單類。 – 2011-06-13 16:39:20

+0

@Rafe,確實如此。 – 2011-06-13 16:55:32

回答

11

Template上的render()方法以Context對象爲參數,而不是字典。您必須從字典中構造一個Context對象,例如

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

可以用'from django.template import context'導入[ – azmeuk 2016-08-24 19:11:50

+2

在更新的版本中'render()'需要一個dict作爲沒有這個方法的上下文 – patroqueeet 2017-08-11 09:20:16

相關問題