简体   繁体   English

django上下文处理器-如何从一个函数返回所有内容?

[英]django context processors - how to return everything from one function?

I have added many context processors to my django test project thinking that adding many, would be OK. 我在django测试项目中添加了很多上下文处理器,以为添加很多就可以了。

But I have been told that I have too many and that each context processors is a "call" and each individual context processor will slow down my test project. 但是有人告诉我,我有太多人了,每个上下文处理器都是一个“电话”,每个单独的上下文处理器都会减慢我的测试项目。 This is true, b/c my test is now really slow. 没错,因为现在我的测试真的很慢。

I have been advised that I can group many of the related context processors as one, thus eliminating many of the calls to just one. 有人告诉我,我可以将许多相关的上下文处理器组合为一个,这样就消除了对一个处理器的许多调用。

But I am unsure how to do this. 但是我不确定该怎么做。

How do I return everything from one function in a single dictionary and then display the required value on the template? 如何从一个字典中的一个函数返回所有内容,然后在模板上显示所需的值?

Here is my common.py file where I declare the subscription prices & the context processors: 这是我的common.py文件,其中声明了订阅价格和上下文处理器:

SUBSCRIPTION_PRICE_FREE = 0
SUBSCRIPTION_PRICE_03MONTHS = 40
SUBSCRIPTION_PRICE_06MONTHS = 60
SUBSCRIPTION_PRICE_12MONTHS = 99

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    ....
    'globalx.core.context_processors.get_subscription_price_free_user',
    'globalx.core.context_processors.get_subscription_price_03Month_user',
    'globalx.core.context_processors.get_subscription_price_06Month_user',
    'globalx.core.context_processors.get_subscription_price_12Month_user',
    'globalx.core.context_processors.get_subscription_price_default',
    ....
}

Here is a section of my context_processors.py file: 这是我的context_processors.py文件的一部分:

....
def get_subscription_price_free_user(request):
    return {'subscription_price_free_user': settings.SUBSCRIPTION_PRICE_FREE}

def get_subscription_price_03Month_user(request):
    return {'subscription_price_03Month_user':
        settings.SUBSCRIPTION_PRICE_03MONTHS}

def get_subscription_price_06Month_user(request):
    return {'subscription_price_06Month_user':
        settings.SUBSCRIPTION_PRICE_06MONTHS}

def get_subscription_price_12Month_user(request):
    return {'subscription_price_12Month_user':
        settings.SUBSCRIPTION_PRICE_12MONTHS}

def get_subscription_price_default(request):
    return {'subscription_price_default': settings.SUBSCRIPTION_PRICE_DEFAULT}
....

Here is how I display the value in the template: 这是我在模板中显示值的方式:

{{ subscription_price_free_user }}
{{ subscription_price_03Month_user }}
{{ subscription_price_06Month_user }}
{{ subscription_price_12Month_user }}
{{ subscription_price_default }}

DOH! DOH!

The answer is so simple and obvious, and I was thinking that it would be deceptively difficult. 答案是如此简单和明显,我一直在想这很难。

Here is my common.py file where I declare the subscription prices & the context processors: 这是我的common.py文件,其中声明了订阅价格和上下文处理器:

SUBSCRIPTION_PRICE_FREE = 0
SUBSCRIPTION_PRICE_03MONTHS = 40
SUBSCRIPTION_PRICE_06MONTHS = 60
SUBSCRIPTION_PRICE_12MONTHS = 99

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    ....
    'globalx.core.context_processors.get_subscription_prices',
    ....

} }

Here is my context_processor.py file: 这是我的context_processor.py文件:

def get_subscription_prices(request):
    return {'subscription_price_free_user': settings.SUBSCRIPTION_PRICE_FREE,
            'subscription_price_03Month_user': settings.SUBSCRIPTION_PRICE_03MONTHS,
            'subscription_price_06Month_user': settings.SUBSCRIPTION_PRICE_06MONTHS,
            'subscription_price_12Month_user': settings.SUBSCRIPTION_PRICE_12MONTHS,
            'subscription_price_default': settings.SUBSCRIPTION_PRICE_DEFAULT}

This is how I call the values in the templates: 这就是我在模板中调用值的方式:

{{ subscription_price_free_user }}
{{ subscription_price_03Month_user }}
{{ subscription_price_06Month_user }}
{{ subscription_price_12Month_user }}
{{ subscription_price_default }}

I hope that this helps some one else. 我希望这对其他人有所帮助。

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

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