librelist archives

« back to archive

Reg-urls

Reg-urls

From:
alice ni
Date:
2011-05-01 @ 12:39
Hi !
Suppose there is a web app name thesite.com. I need to give every user
a url of his own. For eg- if alice signs up, she gets a space of her
own at the url "alice.thesite.com".. How do I achieve this.

Thanks
Alice

Re: [flask] Reg-urls

From:
Simon Sapin
Date:
2011-05-01 @ 13:11
Le 01/05/2011 14:39, alice ni a écrit :
> Hi !
> Suppose there is a web app name thesite.com. I need to give every user
> a url of his own. For eg- if alice signs up, she gets a space of her
> own at the url "alice.thesite.com".. How do I achieve this.
>
> Thanks
> Alice

Hi,

The online documentation seems to be down right now, but the 
@app.route() decorator passes any extra option to the underlying Rule 
object[1]. This includes `subdomain`[2] which can itself contain 
<variables>.

[1] https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L646
[2] 
https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/routing.py#L416

Try something like this:

@app.route('/object<int:id>', subdomain='<username>')
def foo(username, id):
     return 'User %s got object %i.' % (username.title(), id)

http://alice.example.net/object42 should give "User Alice got object 42."

Repeating subdomain='<username>' on every view can be cumbersome. 
Instead try looking at request.path in a @app.before_request function 
and putting the result in g.username, or maybe a WSGI middleware, ...

Of course, in any cases, you need to configure your server to send all 
relevant requests to Flask.

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

Re: [flask] Reg-urls

From:
Ishbir Singh
Date:
2011-05-01 @ 13:21
Ah, that is a nice solution. Didn't know about this. Thanks for this Simon.

On 1 May 2011 18:41, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 01/05/2011 14:39, alice ni a écrit :
> > Hi !
> > Suppose there is a web app name thesite.com. I need to give every user
> > a url of his own. For eg- if alice signs up, she gets a space of her
> > own at the url "alice.thesite.com".. How do I achieve this.
> >
> > Thanks
> > Alice
>
> Hi,
>
> The online documentation seems to be down right now, but the
> @app.route() decorator passes any extra option to the underlying Rule
> object[1]. This includes `subdomain`[2] which can itself contain
> <variables>.
>
> [1] https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L646
> [2]
> https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/routing.py#L416
>
> Try something like this:
>
> @app.route('/object<int:id>', subdomain='<username>')
> def foo(username, id):
>     return 'User %s got object %i.' % (username.title(), id)
>
> http://alice.example.net/object42 should give "User Alice got object 42."
>
> Repeating subdomain='<username>' on every view can be cumbersome.
> Instead try looking at request.path in a @app.before_request function
> and putting the result in g.username, or maybe a WSGI middleware, ...
>
> Of course, in any cases, you need to configure your server to send all
> relevant requests to Flask.
>
> Regards,
> --
> Simon Sapin
> http://exyr.org/
>



-- 
Regards,
Ishbir Singh

Re: [flask] Reg-urls

From:
Ishbir Singh
Date:
2011-05-01 @ 12:55
You'll have to do this with the help of your webserver. Tell it to forward
all URLs of the format *.thesite.com to thesite.com/user/* or something like
this. Lookup the documentation of your web server.

On 1 May 2011 18:09, alice ni <alice.ni19@gmail.com> wrote:

> Hi !
> Suppose there is a web app name thesite.com. I need to give every user
> a url of his own. For eg- if alice signs up, she gets a space of her
> own at the url "alice.thesite.com".. How do I achieve this.
>
> Thanks
> Alice
>



-- 
Regards,
Ishbir Singh