开发者

MEDIA_URL tuple

I would like to write a context_processor, something like this:

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()}

I have MEDIA_URL many places on the template and I'd like each instance to be chosen randomly so that I get a mix of cnames on any given page in order to reduce the number of requests to any given static url at a time. Any thoughts on how best to do thi开发者_如何学Pythons?


Like so, although the exact routine to select the next element is up to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜