2016-10-26 9 views
0

私は、ロードされたテンプレートに特定の変数を渡すことを目指すコンテキストプロセッサを作成しました。Django:コンテキストプロセッサがログインページで実行されていませんか?

これは私が私のコンテキストプロセッサを定義している方法です。

from django.conf import settings 

def theme(request): 
    return {'custom_theme': getattr(settings, "CUSTOM_THEME", "default")} 

これは私が設定ファイルに私のコンテキストプロセッサが含まれている方法です。

TEMPLATES = [{ 
    'BACKEND': 'django.template.backends.django.DjangoTemplates', 
    'APP_DIRS': True, 
    'DIRS': [ 
     normpath(join(PROJECT_PATH, 'templates')), 
    ], 
    'OPTIONS': { 
     'context_processors': { 
      'django.template.context_processors.request', 
      'django.template.context_processors.media', 
      'django.contrib.auth.context_processors.auth', 
      'myapp.context_processor.theme', 
     }, 
     'debug': False, 
    } 
}] 

しかし、何らかの理由で、それがされ主にログインページで実行されるわけではありません。

これは、ログインURLを扱うビューで、「TemplateResponse`戻る:実行中のコンテキストプロセッサは、変数ustom_theme`はコンテキストに挿入されていないと私は取得されていないので

@never_cache 
@sensitive_post_parameters('password') 
@csrf_protect 
def custom_login(request): 

    redirect_field_name = 'next' 
    redirect_to = request.POST.get(redirect_field_name, 
            request.GET.get(redirect_field_name, '')) 

    if request.method == "POST": 
     form = AuthenticationForm(request, data=request.POST) 
     if form.is_valid(): 

      # Ensure the user-originating redirection url is safe. 
      if not is_safe_url(url=redirect_to, host=request.get_host()): 
       redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) 

      # Okay, security check complete. Log the user in. 
      auth_login(request, form.get_user()) 

      if getattr(settings, 'CHECK_PERMISSIONS_ON_CMDB', False): 
       logger.info('Checking permissions on CMDB.') 
       auth_cmdb_user_realms(form.get_user()) 

      return HttpResponseRedirect(redirect_to) 
    else: 
     form = AuthenticationForm(request) 

    context = { 
     'form': form, 
     'version': get_project_version(), 
     redirect_field_name: redirect_to, 
    } 

    if hasattr(settings, "CUSTOM_THEME"): 
     login_template = 'custom_auth/themes/{}/login.html'.\ 
      format(settings.CUSTOM_THEME) 
    else: 
     login_template = 'custom_auth/themes/default/login.html' 

    return TemplateResponse(request, login_template, context) 

を次のエラー:

VariableDoesNotExist: Failed lookup for key [custom_theme] in u"[{'False': False, 'None': None, 'True': True, 'compressed': {'name': None}}]" 

場合によっては、コンテキストプロセッサが実行されていることがあります。

ログインページを読み込もうとしたときにコンテキストプロセッサが実行されていない理由はありますか?

+0

私が最初にあなたが説明してきた1、私に私のreturn文を変更した – e4c5

答えて

1

コンテキストプロセッサは、呼び出さないと使用されません。

長いパス:

return render_to_response("my_app/my_template.html", {'some_var': 'foo'}, 
          context_instance=RequestContext(request)) 

短いパス:

from django.shortcuts import render 

def some_view(request): 
    ...Do something.... 
    return render(request, "MyTemplate.html",{}) 
+0

完全なトレースバックを投稿してください私のログインページの文脈でキー 'some_var'を見ることができません:( – Xar

+0

レンダリングを良くしてください。そして、あなたの答えを 'myapp.context_processor.theme'のコードで更新してください。あなたのhtmlを検索 – Zartch

関連する問題