2012-08-22 33 views
1

不設置請求變種我有以下觀點:Django的上下文處理器在模板

@login_required 
def manage(request): 
    ''' Display the list of posts that belong to the user ''' 
    posts = Post.objects.filter(user=request.user).order_by('created_at') 
    return render_to_response('posts/manage.html', {'posts':posts}, context_instance=RequestContext(request)) 

request變量是在我的模板空。

我國設置:

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth", 
    "django.core.context_processors.debug", 
    "django.core.context_processors.i18n", 
    "django.core.context_processors.media", 
    "django.core.context_processors.static", 
    "django.core.context_processors.tz", 
    "django.contrib.messages.context_processors.messages", 
    'social_auth.context_processors.social_auth_by_name_backends', 
) 

想通過request.user訪問登錄的用戶。

出了什麼問題?謝謝。

請注意,我正在使用社交身份驗證用戶登錄。

回答

6

有了這些背景下處理器,您不必訪問request在所有 - 但你必須user直接訪問。

+1

偉大的答案丹尼爾。 @alexBrand,除非你還需要'request'中的其他東西,Daniel的答案可以爲你節省額外的上下文處理器。 – Brandon

8

添加'django.core.context_processors.request'to your TEMPLATE_CONTEXT_PROCESSORS

相關問題