views:

19

answers:

1
+1  Q: 

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 this?

+1  A: 

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

Ignacio Vazquez-Abrams