简体   繁体   English

Django上下文处理器不会在模板中显示新的上下文值

[英]Django context processor doesn't display new context values in template

EDIT: Updated now that I've narrowed down the problem to the context processor variable not being available to a template that I'm loading with a custom tag. 编辑:现在更新,我将问题范围缩小到上下文处理器变量,该变量不适用于使用自定义标记加载的模板。

I'm using Django 1.11 and this is my first time trying to use a custom context processor. 我正在使用Django 1.11,这是我第一次尝试使用自定义上下文处理器。

The problem is that the context variable I'm supposed to be adding from the context processor does not return anything from within a template loaded from a custom tag. 问题是应该从上下文处理器添加的上下文变量不会从自定义标签加载的模板中返回任何内容。 I don't receive any errors. 我没有收到任何错误。

So below {{ testcontext }} should return "IT WORKED!" 因此,在{{testcontext}}下面应返回“ IT WORKED!”。 and does so in my base.html template, but returns nothing in the template loaded with @register.inclusion_tag(). 并在我的base.html模板中执行此操作,但是在加载@ register.inclusion_tag()的模板中不返回任何内容。

settings.py: settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
                'appname.context_processors.test_context',
            ],
        },
    },
]

context_processors.py: context_processors.py:

def test_context(request):
    return {'testcontext': 'TEST WORKED!'}

tags.py tags.py

from django import template

from appname.models import Category

register = template.Library()

@register.inclusion_tag('template.html')
def load_category(selected_slug=None):
    return {
        'categories': Category.objects.all(),
        'selected':selected_slug,
    }

views.py: views.py:

from django.views.generic import ListView
from appname.models import MyModel

class MyView(ListView):
    model = MyModel

urls.py urls.py

from django.conf.urls import url

from appname.views import MyView

urlpatterns = [
    url(r'^$', MyView.as_view(), name="home"),
]

template.html template.html

{{ testcontext }}

So the problem was in my custom tag not carrying over the context when loading template.html. 因此,问题出在我的自定义标记中,当加载template.html时,该标记未保留上下文。 So the below code fixes it and my variable from the context processor is now working as expected. 因此,以下代码对其进行了修复,并且上下文处理器中的我的变量现在可以按预期运行。

tags.py tags.py

from django import template

from appname.models import Category

register = template.Library()

@register.inclusion_tag('template.html', takes_context=True)
def load_category(context,selected_slug=None):
    return {
        'categories': Category.objects.all(),
        'selected': selected_slug,
        'testcontext': context['testcontext'] 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM