Reputation: 3695
I have added many context processors to my django test project thinking that adding many, would be OK.
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:
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:
....
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 }}
Upvotes: 1
Views: 307
Reputation: 3695
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:
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:
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.
Upvotes: 1