2016-01-12 42 views
0

我對現有的代碼庫中引用,看到這一段代碼:使用當地人在Django的意見和全局變量可以在模板

from constants import AppType 
""" 
AppType is a constant class in constants.py and it goes like 
AppType: 
    TypeA = 1 
    TypeB = 2 
""" 
@log_request(log_response=False) 
@user_profile_required 
def app_home(request, user, user_profile): 
    apps = db_manager.get_all_apps_for_user(
     user_profile.uid, user_profile.platform 
    ) 
    return render(request, 'appinfo/all_apps.html', locals()) 
    # notice the use of locals here for convinience 

all_apps.html

{% for val, name in app_types.items %} 
{{val}}, {{name}}<br> 
{% endfor %} 

和我看到這個迴應時呈現出來:

1, TypeA 
2, TypeB 

根據我的理解(糾正我,如果我錯誤),locals應該返回局部變量的字典。但顯然在視圖函數中沒有app_types

我懷疑有一些魔法將AppType轉換爲app_types,我將TypeC = 3轉換爲AppType。然後我確實看到3, TypeC呈現出來。因此,我的嫌疑人似乎是真的?

這裏有什麼竅門,或者我缺少一些其他信息來理解這個問題?

回答

0

那麼,原來定義的app_types是在context_processors.py並且加載到TEMPLATE_CONTEXT_PROCESSORSsettings.py

所以這只是對Django模板系統及其處理的理解問題