2009-10-06 66 views
4

我一定錯過了設置自定義模板上下文的事情,因爲它永遠不會被調用。爲什麼我的django模板上下文處理器沒有被調用

在設置:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth", 
    "django.core.context_processors.debug", 
    "django.core.context_processors.i18n", 
    "django.core.context_processors.media", 
    "django_authopenid.context_processors.authopenid", 
    "web.context_processors.my_hat", 
) 
在網絡

/context_processors.py

from libs.utils import get_hat, get_project, my_hats 

print 'heloooo' 

def my_hat(request): 
    """Insert some additional information into the template context 
    """ 
   
    import pdb 
    pdb.set_trace() 

    print 'hiiiiiiii' 

    return {'hat': get_hat(request), 
     'project': get_project(request), 
     } 

毫無輸出和Django的進程查看和顯示模板不會去訪問這一點。我錯過了什麼!?

感謝Insin,位我錯過了:

在view.py

return render_to_response(template, { 
     'tasks': tasks, 
    }, 
    context_instance=RequestContext(request)) 

在模板:

My current hat is {{hat}} 

回答

14

你記得使用RequestContext渲染時模板?

由於Django的1.3,有一個新的快捷功能,render,它默認使用RequestContext:從我的廣泛的谷歌搜索的例子

return render(request, template, { 
    'tasks': tasks, 
}) 
+0

不作爲沒有表現出來! http://stackoverflow.com/questions/557460/django-having-middleware-communicate-with-views-templates http://stackoverflow.com/questions/1025025/django-context-processor-trouble http:///lethain.com/zh-CN/entry/2007/jun/14/a-django-middleware-for-google-analytics-repost/ ... – PhoebeB 2009-10-07 13:22:28

相關問題