Джанго версия 1,8Django - Почему context.get ('request') возвращает значение None?
@register.simple_tag(takes_context=True)
def current(context, url_name):
print(context)
print(context.get('request'))
current_path = context.get('request').path
return 'active' if current_path.startswith(url_name) else ''
>>> [{'True': True, 'False': False, 'None': None}, {'csrf_token': <django.utils.functional.lazy.<locals>.__proxy__ object at 0x7f61d288b860>, 'links': OrderedDict([('index', 'home'), ('products', 'prodotti'), ('contact_us', 'contattaci')])}, {'forloop': {'counter0': 2, 'revcounter': 1, 'revcounter0': 0, 'counter': 3, 'parentloop': {}, 'last': True, 'first': False}}, {'name': 'contattaci', 'key': 'contact_us'}]
>>> None
Реакция второй печати не является на самом деле ни один, но я не могу понять причину. Похоже, что объект «запрос» не существует или вообще недоступен.
Это интересующая деталь в файле settings.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Первая печать соответствует 'print (context)'? – Gocht
@Gocht Ya, было довольно ясно .. –
Если это так, вы должны получить 'AttributeError', потому что в« списке »нет метода' get'. И, как видите, 'print (context)' печатает 'list'. – Gocht