Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 7, 2010 14:35:56

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Hey guys,

I wanted to ask if there's any way or accepted solution for using more
than one URL simultaneously as the MEDIA_URL, so as to enable browsers
to further parallelize the download of static content. That is, having
something like:

MEDIA_URL =

I've been searching through the group, documentation and online but
couldn't find anything that addressed the subject.

Thanks,
Martin

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 7, 2010 14:48:37

Steven L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Hi Martin-

I don't know what the "official" answer would be, but you could write your own
context processor that had something like:

from random import choice
MEDIA_URLS = 'static1.site.com', 'static2.site.com', 'static3.site.com' ]
def media(request):
return {'MEDIA_URL': choice(MEDIA_URLS)}

Then, in settings.py, include your custom context processor instead of the one
built-in to to Django.

-Steve



====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085 | ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 8, 2010 13:33:00

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Steve,

Thanks for the answer.

If I understand correctly your idea and code, each template would have
only one value for the MEDIA_URL (randomly chosen from the array),
right?

What I was looking for was a way of distributing the value of the
MEDIA_URL uniformly along the values of an array, in each template. So
in a certain template the MEDIA_URL would have more than one value.
Maybe if I set it as a callable (with the logic that choses the values
inside it) that would work.

I cannot quite understand why there isn't anymore people running into
this issue.

Best and thanks again for the answer,
Martin

On Jun 7, 10:46 am, Steven L Smith <ssmit...@naz.edu> wrote:
> Hi Martin-
>
> I don't know what the "official" answer would be, but you could write your own
> context processor that had something like:
>
> from random import choice
> MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> def media(request):
>     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> Then, in settings.py, include your custom context processor instead of the one
> built-in to to Django.
>
> -Steve
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085   |   ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 8, 2010 15:01:28

Steven L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Hi Martin-

Yes, that is what that code would do... I guess I forgot about how context
processors work. :-P

I think most people do this type of thing with a round-robin server setup. If
you wanted to do it django-side, you'd probably need to do a template tag.
Note the callable...

-Steve



random_media.py
-----------------------------
from django import template
from random import choice

register = template.Library()

def media_url():
media_urls =
return choice(media_urls)
register.simple_tag(media_url)


in some template
------------------
{% load random_media %}
{% media_url %}





====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085 | ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46====================================

----- Original Message -----
From: "Martin Siniawski" <msi...@gmail.com>
To: "Django users" <django-users@googlegroups.com>
Sent: Tuesday, June 8, 2010 8:32:53 AM GMT -05:00 US/Canada Eastern
Subject: Re: Using array of URLs for MEDIA_URL

Steve,

Thanks for the answer.

If I understand correctly your idea and code, each template would have
only one value for the MEDIA_URL (randomly chosen from the array),
right?

What I was looking for was a way of distributing the value of the
MEDIA_URL uniformly along the values of an array, in each template. So
in a certain template the MEDIA_URL would have more than one value.
Maybe if I set it as a callable (with the logic that choses the values
inside it) that would work.

I cannot quite understand why there isn't anymore people running into
this issue.

Best and thanks again for the answer,
Martin

On Jun 7, 10:46 am, Steven L Smith <ssmit...@naz.edu> wrote:
> Hi Martin-
>
> I don't know what the "official" answer would be, but you could write your
own
> context processor that had something like:
>
> from random import choice
> MEDIA_URLS = 'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> def media(request):
> return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> Then, in settings.py, include your custom context processor instead of the
one
> built-in to to Django.
>
> -Steve
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085 | ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 8, 2010 15:03:29

Steven L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Looks like my email client screwed up the indentation.... I've added some
indents...

random_media.py
-----------------------------
from django import template
from random import choice

register = template.Library()

def media_url():
media_urls =
return choice(media_urls)
register.simple_tag(media_url)


in some template
------------------
{% load random_media %}
{% media_url %}





====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085 begin_of_the_skype_highlighting 585-389-2085
end_of_the_skype_highlighting | ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 8, 2010 15:30:03

Alex R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


I haven't tested this, but it seems like you could do it one of two
ways:

Define media root as a function in your settings.py. If you pass a
callable to the template, it will be called.
MEDIA_ROOTS =
def MEDIA_ROOT():
from random import choice
return choice(MEDIA_ROOTS)

OR

Use a context processor to add MEDIA_ROOTS to the context and use the
random template tag.

{{ MEDIA_ROOTS|random }}


Steven's custom template tag works too.

Alex
On Jun 8, 7:32 am, Martin Siniawski <msi...@gmail.com> wrote:
> Steve,
>
> Thanks for the answer.
>
> If I understand correctly your idea and code, each template would have
> only one value for the MEDIA_URL (randomly chosen from the array),
> right?
>
> What I was looking for was a way of distributing the value of the
> MEDIA_URL uniformly along the values of an array, in each template. So
> in a certain template the MEDIA_URL would have more than one value.
> Maybe if I set it as a callable (with the logic that choses the values
> inside it) that would work.
>
> I cannot quite understand why there isn't anymore people running into
> this issue.
>
> Best and thanks again for the answer,
> Martin
>
> On Jun 7, 10:46 am, Steven L Smith <ssmit...@naz.edu> wrote:
>
> > Hi Martin-
>
> > I don't know what the "official" answer would be, but you could write your
> > own
> > context processor that had something like:
>
> > from random import choice
> > MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> > def media(request):
> >     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> > Then, in settings.py, include your custom context processor instead of the
> > one
> > built-in to to Django.
>
> > -Steve
>
> > ====================================
> > Steven L Smith, Web Developer
> > Department of Information Technology Services
> > Nazareth College of Rochester
> > 585-389-2085   |   ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> > ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 June 11, 2010 19:09:50

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Steven,

Thanks again for the answer.

I couldn't quite understand the comment regarding the round robin
server setup. In order to workaround the browser's limitations to
simultaneous connections that can be opened to a host, the links to
static content in the HTML content must be to different hosts. How
would that be done with the round robin server setup you mentioned?

Thanks for the proposed solution as well. It seems to be a good
solution, but it would imply changing all the templates, and I don't
even know what would happen to those templates that are from external
apps.

I'll post a solution that I coded on my own, though I haven't yet
benchmarked it.

Best,
Martin

On Jun 8, 11:01 am, Steven L Smith <ssmit...@naz.edu> wrote:
> Looks like my email client screwed up the indentation.... I've added some
> indents...
>
> random_media.py
> -----------------------------
> from django import template
> from random import choice
>
> register = template.Library()
>
> defmedia_url():
>     media_urls =
>     return choice(media_urls)
> register.simple_tag(media_url)
>
> in some template
> ------------------
> {% load random_media %}
> {%media_url%}
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085 begin_of_the_skype_highlighting              585-389-2085      
> end_of_the_skype_highlighting |
> ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 June 11, 2010 19:22:40

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Steven and Alex,

This is what I coded, and tried it out and works. I still have to
benchmark it.

from django.conf import settings

class RoundRobinMediaUrl():
current_url = 0
try:
media_urls = settings.MEDIA_URLS
except:
media_urls = (settings.MEDIA_URL,)

def __unicode__(self):
self.current_url += 1
self.current_url %= len(self.media_urls)
return self.media_urls

def context_proc (req):
return {'MEDIA_URL': RoundRobinMediaUrl()}

On Jun 11, 3:09 pm, Martin Siniawski <msi...@gmail.com> wrote:
> Steven,
>
> Thanks again for the answer.
>
> I couldn't quite understand the comment regarding the round robin
> server setup. In order to workaround the browser's limitations to
> simultaneous connections that can be opened to a host, the links to
> static content in the HTML content must be to different hosts. How
> would that be done with the round robin server setup you mentioned?
>
> Thanks for the proposed solution as well. It seems to be a good
> solution, but it would imply changing all the templates, and I don't
> even know what would happen to those templates that are from external
> apps.
>
> I'll post a solution that I coded on my own, though I haven't yet
> benchmarked it.
>
> Best,
> Martin
>
> On Jun 8, 11:01 am, Steven L Smith <ssmit...@naz.edu> wrote:
>
>
>
> > Looks like my email client screwed up the indentation.... I've added some
> > indents...
>
> > random_media.py
> > -----------------------------
> > from django import template
> > from random import choice
>
> > register = template.Library()
>
> > defmedia_url():
> >     media_urls =
> >     return choice(media_urls)
> > register.simple_tag(media_url)
>
> > in some template
> > ------------------
> > {% load random_media %}
> > {%media_url%}
>
> > ====================================
> > Steven L Smith, Web Developer
> > Department of Information Technology Services
> > Nazareth College of Rochester
> > 585-389-2085 begin_of_the_skype_highlighting              585-389-2085      
> > end_of_the_skype_highlighting |
> > ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> > ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 June 11, 2010 19:24:24

Martin S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using array of URLs for MEDIA_URL


Alex,

Thanks for the answer.

I tried doing something like the first thing you proposed (MEDIA_URL
as a callable) but I had to tweak it a little bit because the template
wasn't calling the callable, but rather printing 'function
object .....'.

What do you think of the solution I posted?

Best,
Martin

On Jun 8, 11:29 am, Alex Robbins <alexander.j.robb...@gmail.com>
wrote:
> I haven't tested this, but it seems like you could do it one of two
> ways:
>
> Define media root as a function in your settings.py. If you pass a
> callable to the template, it will be called.
> MEDIA_ROOTS =
> def MEDIA_ROOT():
>     from random import choice
>     return choice(MEDIA_ROOTS)
>
> OR
>
> Use a context processor to add MEDIA_ROOTS to the context and use the
> random template tag.
>
> {{ MEDIA_ROOTS|random }}
>
> Steven's custom template tag works too.
>
> Alex
> On Jun 8, 7:32 am, Martin Siniawski <msi...@gmail.com> wrote:
>
>
>
> > Steve,
>
> > Thanks for the answer.
>
> > If I understand correctly your idea and code, each template would have
> > only one value for the MEDIA_URL (randomly chosen from the array),
> > right?
>
> > What I was looking for was a way of distributing the value of the
> > MEDIA_URL uniformly along the values of an array, in each template. So
> > in a certain template the MEDIA_URL would have more than one value.
> > Maybe if I set it as a callable (with the logic that choses the values
> > inside it) that would work.
>
> > I cannot quite understand why there isn't anymore people running into
> > this issue.
>
> > Best and thanks again for the answer,
> > Martin
>
> > On Jun 7, 10:46 am, Steven L Smith <ssmit...@naz.edu> wrote:
>
> > > Hi Martin-
>
> > > I don't know what the "official" answer would be, but you could write
> > > your own
> > > context processor that had something like:
>
> > > from random import choice
> > > MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> > > def media(request):
> > >     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> > > Then, in settings.py, include your custom context processor instead of
> > > the one
> > > built-in to to Django.
>
> > > -Steve
>
> > > ====================================
> > > Steven L Smith, Web Developer
> > > Department of Information Technology Services
> > > Nazareth College of Rochester
> > > 585-389-2085   |   ssmit...@naz.eduhttp://www.naz.edu/pub/~ssmith46> > > ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support