librelist archives

« back to archive

Flask-Classy url_for

Flask-Classy url_for

From:
Luis Fernando Barrera
Date:
2013-02-03 @ 10:19
I'm trying to use Flask Classy

I wonder if there's a way to use url_for within it. If so, how do you refer
the endpoint?


class AuthView(FlaskView):
  def index(self):
    return "Hello"

  def post(self):
    if request.form['username']:
      return "Hello " + request.form['username']
    else:
      return redirect(url_for('.index'))

AuthView.register(app, route_base="/auth")





Luis Fernando Barrera
luisfernando@informind.com

Re: Flask-Classy url_for

From:
Luis Fernando Barrera
Date:
2013-02-03 @ 10:30
I just added @route('/index', endpoint="index") and I can refer to the
endpoint within the class.

How do I refer to that endpoint in an external FlaskView?

class AuthView(FlaskView):
  @route('/index', endpoint="index")
  def index(self):
    return "Hello"

  def post(self):
    if request.form['username']:
      return "Hello " + request.form['username']
    else:
      return redirect(url_for('.index'))

class AnotherFlaskView(FlaskView)
  def index(self):
    return redirect(url_for('AuthView.index'))


Luis Fernando Barrera
luisfernando@informind.com



2013/2/3 Luis Fernando Barrera <luisfernando@informind.com>

> I'm trying to use Flask Classy
>
> I wonder if there's a way to use url_for within it. If so, how do you
> refer the endpoint?
>
>
> class AuthView(FlaskView):
>   def index(self):
>     return "Hello"
>
>   def post(self):
>     if request.form['username']:
>       return "Hello " + request.form['username']
>     else:
>       return redirect(url_for('.index'))
>
> AuthView.register(app, route_base="/auth")
>
>
>
>
>
> Luis Fernando Barrera
> luisfernando@informind.com
>
>
>

Re: [flask] Re: Flask-Classy url_for

From:
Freedom Dumlao
Date:
2013-02-04 @ 02:55
Hi Luis -

When you use the `endpoint=` parameter it sets the endpoint explicitly. So
in the example you show:

  @route('/index', endpoint="index")
  def index(self):
    return "Hello"

you should be able to get that endpoint using `url_for('index')`.

If you don't set the endpoint explicitly:

  @route('/index')
  def index(self):
    return "Hello"

the endpoint should be `AnotherView:index`

Let me know if you have any questions and if it doesn't work - please
submit a bug at https://github.com/apiguy/flask-classy

- Freedom

On Sun, Feb 3, 2013 at 5:30 AM, Luis Fernando Barrera <
luisfernando@informind.com> wrote:

> I just added @route('/index', endpoint="index") and I can refer to the
> endpoint within the class.
>
> How do I refer to that endpoint in an external FlaskView?
>
> class AuthView(FlaskView):
>   @route('/index', endpoint="index")
>   def index(self):
>     return "Hello"
>
>   def post(self):
>     if request.form['username']:
>       return "Hello " + request.form['username']
>     else:
>       return redirect(url_for('.index'))
>
> class AnotherFlaskView(FlaskView)
>   def index(self):
>     return redirect(url_for('AuthView.index'))
>
>
> Luis Fernando Barrera
> luisfernando@informind.com
>
>
>
> 2013/2/3 Luis Fernando Barrera <luisfernando@informind.com>
>
>> I'm trying to use Flask Classy
>>
>> I wonder if there's a way to use url_for within it. If so, how do you
>> refer the endpoint?
>>
>>
>> class AuthView(FlaskView):
>>   def index(self):
>>     return "Hello"
>>
>>   def post(self):
>>     if request.form['username']:
>>       return "Hello " + request.form['username']
>>     else:
>>       return redirect(url_for('.index'))
>>
>> AuthView.register(app, route_base="/auth")
>>
>>
>>
>>
>>
>> Luis Fernando Barrera
>> luisfernando@informind.com
>>
>>
>>
>