This is a context processor that will allow you to cycle the values of your MEDIA_URL context variable. It will cycle through the urls defined in settings.MEDIA_URLS, so that you can distribute your media url's between multiple servers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def media_urls(request):
media_urls = URLCycle(url_list=settings.MEDIA_URLS)
return {'MEDIA_URL':media_urls}
class URLCycle(object):
def __init__(self,url_list):
self.urls = url_list
self.iter = 0
def __str__(self):
if self.iter == len(self.urls):
self.iter = 0
url = self.urls[self.iter]
self.iter += 1
return url
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Your code is much more compact - and nicer, too! Thanks for pointing it out to me - I didn't realize that there was something built-in to cycle through things for me.
#
Just an update on the above - you'd probably want to hit each subdomain at least twice so:
#
Please login first before commenting.