我想写一个context_processor,类似这样:
settings.py:
MEDIA_URLS = ('cname2.example.com/media', 'cname3.example.com/media',)
TEMPLATE_CONTEXT_PROCESSORS = (
"util.context_processors.media",
)
UTIL / context_processors.py
from random import choice
from django.conf import settings
def media(request):
"""
Adds random media-related context variable to the context.
"""
def get_media_url():
return choice(settings.MEDIA_URLS)
return {'MEDIA_URL': get_media_url()}
我在模板上有很多地方MEDIA_URL,我希望随机选择每个实例,以便在任何给定页面上混合使用cnames,以便一次减少对任何给定静态URL的请求数量。有关如何最好地做到这一点的任何想法?