librelist archives

« back to archive

2 routes to one view

2 routes to one view

From:
lynadge
Date:
2012-05-06 @ 11:10
Hey FlaskLister, i got a little question.

How can i add 2 routes to one view? The simple way is with @route('/') 
and another @route('/foo') to the view.

But what if i have one route in a blueprint, for example a 'news' 
blueprint @bp.route('/news/') and want to have a second route with the 
same view in my app outside the blueprint, for example @app.route('/').

I can't figure out the way how i should do it. I tried with 
add_url_rule but it didn't work ... or i use it in a wrong way.

My (i think) wrong example:

app.add_url_rule('/', app.blueprints['news']...)


My thoughts about how it should work:

@app.route('/')
@bp.route('/news/')
def news():
     return 'Foobar News'

I hope you can follow me and understand my crappy english.

Re: [flask] 2 routes to one view

From:
Pronoy Chopra
Date:
2012-05-06 @ 11:26
I think you can just do

>
> @app.route('/')
> @bp.route('/news/')
> def news():
> return 'Foobar News'


@app.route('/')
def foobar:
<some condition>
return redirect(url_for('news'))

Re: [flask] 2 routes to one view

From:
lynadge
Date:
2012-05-06 @ 12:13
this redirect from '/' to '/news', right?

This is not my goal. I want that the user can access from both routes 
the view without a redirect.

Am 06.05.2012 12:26, schrieb Pronoy Chopra:
> I think you can just do
>
>> @app.route('/')
>> @bp.route('/news/')
>> def news():
>> return 'Foobar News'
>
>  @app.route('/')
>  def foobar:
>  <some condition>
>  return redirect(url_for('news'))

Re: [flask] 2 routes to one view

From:
pronoyc@gmail.com
Date:
2012-05-06 @ 11:39
>
>
> @app.route('/')
> def foobar:
>    <some condition>
>    return redirect(url_for('news'))
>

Here's the 
example<http://flask.pocoo.org/docs/tutorial/views/#login-and-logout>you
can take as a reference

Pronoy Chopra

Re: [flask] 2 routes to one view

From:
lynadge
Date:
2012-05-06 @ 12:54
Ok, i solved it with this simple snippet:

@app.route('/')
def index():
     from app.views.news import news
     return news.news(None)

Is this a useful approach? and, above all, is it an effective way?

Am 06.05.2012 12:10, schrieb lynadge:
> Hey FlaskLister, i got a little question.
>
> How can i add 2 routes to one view? The simple way is with 
> @route('/')
> and another @route('/foo') to the view.
>
> But what if i have one route in a blueprint, for example a 'news'
> blueprint @bp.route('/news/') and want to have a second route with 
> the
> same view in my app outside the blueprint, for example 
> @app.route('/').
>
> I can't figure out the way how i should do it. I tried with
> add_url_rule but it didn't work ... or i use it in a wrong way.
>
> My (i think) wrong example:
>
> app.add_url_rule('/', app.blueprints['news']...)
>
>
> My thoughts about how it should work:
>
> @app.route('/')
> @bp.route('/news/')
> def news():
>      return 'Foobar News'
>
> I hope you can follow me and understand my crappy english.