开发者

MEDIA_URL tuple

开发者 https://www.devze.com 2023-01-19 08:46 出处:网络
I would like to write a context_processor, something like this: settings.py: MEDIA_URLS = (\'cname2.example.com/media\', \'cname3.example.com/media\',)

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

精彩评论

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