librelist archives

« back to archive

Dynamic subdomain

Dynamic subdomain

From:
Nicolas Clairon
Date:
2011-02-22 @ 14:09
Hi,

Is there a way to implement dynamic subdomain with Flask ? I'd like to
do something like that:

python.example.com
apple.example.com
-> get the subdomain name and do something with that

instead of
example.com/apple

N.

Re: [flask] Dynamic subdomain

From:
Simon Sapin
Date:
2011-02-22 @ 14:35
Le 22/02/2011 23:09, Nicolas Clairon a écrit :
> Hi,
>
> Is there a way to implement dynamic subdomain with Flask ? I'd like to
> do something like that:
>
> python.example.com
> apple.example.com
> ->  get the subdomain name and do something with that
>
> instead of
> example.com/apple

Hi,

Extra parameters you pass to @app.route() and friends are passed 
directly to Werkzeug’s Rule class:
http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule

You can restrict a rule to a given subdomain pattern, and this pattern 
can contain variable parts the same way that URL patterns do:

@app.route('/foo', subdomain='<bar>')
def foo(bar):
# ...

Regards,
-- 
Simon Sapin
http://exyr.org

Re: [flask] Dynamic subdomain

From:
Nicolas Clairon
Date:
2011-02-22 @ 15:51
Hi,

Thank you for your answers. The Site object is pretty interesting.

About passing the subdomain to the route, this is what I expected. But
I tried to implement it in a simple app without success :

http://paste.pocoo.org/show/342835/

This example don't work either on

fr.localhost:5000
en.localhost:5000
or
localhost:5000

Am I missing something ?

N.

On Tue, Feb 22, 2011 at 3:35 PM, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 22/02/2011 23:09, Nicolas Clairon a écrit :
>> Hi,
>>
>> Is there a way to implement dynamic subdomain with Flask ? I'd like to
>> do something like that:
>>
>> python.example.com
>> apple.example.com
>> ->  get the subdomain name and do something with that
>>
>> instead of
>> example.com/apple
>
> Hi,
>
> Extra parameters you pass to @app.route() and friends are passed
> directly to Werkzeug’s Rule class:
> http://werkzeug.pocoo.org/docs/routing/#werkzeug.routing.Rule
>
> You can restrict a rule to a given subdomain pattern, and this pattern
> can contain variable parts the same way that URL patterns do:
>
> @app.route('/foo', subdomain='<bar>')
> def foo(bar):
> # ...
>
> Regards,
> --
> Simon Sapin
> http://exyr.org
>

Re: [flask] Dynamic subdomain

From:
Julen Ruiz Aizpuru
Date:
2011-02-22 @ 16:11
ar., 2011.eko otsren 22a 16:51(e)an, Nicolas Clairon(e)k idatzi zuen:
> Hi,
>
> Thank you for your answers. The Site object is pretty interesting.
>
> About passing the subdomain to the route, this is what I expected. But
> I tried to implement it in a simple app without success :
>
> http://paste.pocoo.org/show/342835/
>
> This example don't work either on
>
> fr.localhost:5000
> en.localhost:5000
> or
> localhost:5000
>
> Am I missing something ?
>

Your example works fine if you add fr.localhost and en.localhost to your 
/etc/hosts file.

j.

Re: [flask] Dynamic subdomain

From:
Nicolas Clairon
Date:
2011-02-22 @ 16:18
Gosh ! I forgot to make the changes. I think it's coffee time :)

Thank you for your helpful answers.

On Tue, Feb 22, 2011 at 5:11 PM, Julen Ruiz Aizpuru <julenx@gmail.com> wrote:
> ar., 2011.eko otsren 22a 16:51(e)an, Nicolas Clairon(e)k idatzi zuen:
>> Hi,
>>
>> Thank you for your answers. The Site object is pretty interesting.
>>
>> About passing the subdomain to the route, this is what I expected. But
>> I tried to implement it in a simple app without success :
>>
>> http://paste.pocoo.org/show/342835/
>>
>> This example don't work either on
>>
>> fr.localhost:5000
>> en.localhost:5000
>> or
>> localhost:5000
>>
>> Am I missing something ?
>>
>
> Your example works fine if you add fr.localhost and en.localhost to your
> /etc/hosts file.
>
> j.
>

Re: [flask] Dynamic subdomain

From:
Paul Burt
Date:
2011-02-22 @ 14:18
Create a context processor that injects 'request.host' into your
templates and branch accordingly.

For more control, you could create a Site object, instantiated from
the current request, and add properties to that, for example:

class Site(object):
  def __init__(self, request):
    self.host = request.host

  @cached_property
  def google_analytics_id(self, default=''):
    if self.host == 'python.example.com':
      return <something>
    elif self.host == 'apple.example.com':
      return <something else>
    return default

Then use site = Site(request) in your context processor and refer to
site.<attribute> in your templates. Candidates for other properties
might be HTML meta description and keywords, the site's title etc.
This kind of branching is only possible from parts of the application
that have access to the request object, of course.

Paul



On 22 February 2011 14:09, Nicolas Clairon <clairon@gmail.com> wrote:
> Hi,
>
> Is there a way to implement dynamic subdomain with Flask ? I'd like to
> do something like that:
>
> python.example.com
> apple.example.com
> -> get the subdomain name and do something with that
>
> instead of
> example.com/apple
>
> N.
>