librelist archives

« back to archive

Turn off strict_slashes on all routes

Turn off strict_slashes on all routes

From:
Matthew Lewis
Date:
2013-06-19 @ 19:31
Hello,

I've got a Flask script looking like this so far:

@app.route('/')
def index():
    project_list = projects.get_projects()
    return render_template('index.html', projects=project_list)

@app.route('/blog', strict_slashes=False)
def blog():
    ... do things ...

@app.route('/<project>', strict_slashes=False)
def project(project):
    ... do things ...

We plan on adding a lot more routes and setting strict_slashes=False
on all of them. Is there any way to set the default strict_slashes
setting to False and override to True on specific routes?

Thanks,
Matt

Re: [flask] Turn off strict_slashes on all routes

From:
Date:
2013-06-20 @ 18:27
app = Flask(__name__)
app.url_map.strict_slashes = False

19.06.2013, 23:33, "Matthew Lewis" <matt@mplewis.com>:
> Hello,
>
> I've got a Flask script looking like this so far:
>
> @app.route('/')
> def index():
>     project_list = projects.get_projects()
>     return render_template('index.html', projects=project_list)
>
> @app.route('/blog', strict_slashes=False)
> def blog():
>     ... do things ...
>
> @app.route('/<project>', strict_slashes=False)
> def project(project):
>     ... do things ...
>
> We plan on adding a lot more routes and setting strict_slashes=False
> on all of them. Is there any way to set the default strict_slashes
> setting to False and override to True on specific routes?
>
> Thanks,
> Matt

Re: [flask] Turn off strict_slashes on all routes

From:
Matthew Lewis
Date:
2013-06-20 @ 19:45
This is exactly what I've been looking for, thanks!

On Thu, Jun 20, 2013 at 11:27 AM,  <mike@yumatov.org> wrote:
> app = Flask(__name__)
> app.url_map.strict_slashes = False
>
> 19.06.2013, 23:33, "Matthew Lewis" <matt@mplewis.com>:
>> Hello,
>>
>> I've got a Flask script looking like this so far:
>>
>> @app.route('/')
>> def index():
>>     project_list = projects.get_projects()
>>     return render_template('index.html', projects=project_list)
>>
>> @app.route('/blog', strict_slashes=False)
>> def blog():
>>     ... do things ...
>>
>> @app.route('/<project>', strict_slashes=False)
>> def project(project):
>>     ... do things ...
>>
>> We plan on adding a lot more routes and setting strict_slashes=False
>> on all of them. Is there any way to set the default strict_slashes
>> setting to False and override to True on specific routes?
>>
>> Thanks,
>> Matt

Re: [flask] Turn off strict_slashes on all routes

From:
Steven Kryskalla
Date:
2013-06-19 @ 20:41
On Wed, Jun 19, 2013 at 12:31 PM, Matthew Lewis <matt@mplewis.com> wrote:
> Hello,
>
> I've got a Flask script looking like this so far:
>
> @app.route('/')
> def index():
>     project_list = projects.get_projects()
>     return render_template('index.html', projects=project_list)
>
> @app.route('/blog', strict_slashes=False)
> def blog():
>     ... do things ...
>
> @app.route('/<project>', strict_slashes=False)
> def project(project):
>     ... do things ...
>
> We plan on adding a lot more routes and setting strict_slashes=False
> on all of them. Is there any way to set the default strict_slashes
> setting to False and override to True on specific routes?

You could wrap app.route with your own behavior this way:

def route(*a, **kw):
    kw['strict_slashes'] = kw.get('strict_slashes', True)
    return app.route(*a, **kw)

Then use @route instead of @app.route to get the behavior of
strict_slashes defaulting to True.

-Steve

Re: [flask] Turn off strict_slashes on all routes

From:
Andy Wilson
Date:
2013-06-19 @ 22:34
On Wed, Jun 19, 2013 at 3:41 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Wed, Jun 19, 2013 at 12:31 PM, Matthew Lewis <matt@mplewis.com> wrote:
> > Hello,
> >
> > I've got a Flask script looking like this so far:
> >
> > @app.route('/')
> > def index():
> >     project_list = projects.get_projects()
> >     return render_template('index.html', projects=project_list)
> >
> > @app.route('/blog', strict_slashes=False)
> > def blog():
> >     ... do things ...
> >
> > @app.route('/<project>', strict_slashes=False)
> > def project(project):
> >     ... do things ...
> >
> > We plan on adding a lot more routes and setting strict_slashes=False
> > on all of them. Is there any way to set the default strict_slashes
> > setting to False and override to True on specific routes?
>
> You could wrap app.route with your own behavior this way:
>
> def route(*a, **kw):
>     kw['strict_slashes'] = kw.get('strict_slashes', True)
>     return app.route(*a, **kw)
>
> Then use @route instead of @app.route to get the behavior of
> strict_slashes defaulting to True.
>
> -Steve
>


Yeah, adding a new decorator is cleaner than subclassing the whole app
since it limits it's effects to just the new decorator. You could even use
functools.partial:
    route = functools.partial(app.route, strict_slashes=False)

-andy

Re: [flask] Turn off strict_slashes on all routes

From:
Igor Davydenko
Date:
2013-06-20 @ 16:55
+1 to Andy Wilson's solution, functools.partial created especiall for these
cases.


On 20 June 2013 01:34, Andy Wilson <wilson.andrew.j@gmail.com> wrote:

>
> On Wed, Jun 19, 2013 at 3:41 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:
>
>> On Wed, Jun 19, 2013 at 12:31 PM, Matthew Lewis <matt@mplewis.com> wrote:
>> > Hello,
>> >
>> > I've got a Flask script looking like this so far:
>> >
>> > @app.route('/')
>> > def index():
>> >     project_list = projects.get_projects()
>> >     return render_template('index.html', projects=project_list)
>> >
>> > @app.route('/blog', strict_slashes=False)
>> > def blog():
>> >     ... do things ...
>> >
>> > @app.route('/<project>', strict_slashes=False)
>> > def project(project):
>> >     ... do things ...
>> >
>> > We plan on adding a lot more routes and setting strict_slashes=False
>> > on all of them. Is there any way to set the default strict_slashes
>> > setting to False and override to True on specific routes?
>>
>> You could wrap app.route with your own behavior this way:
>>
>> def route(*a, **kw):
>>     kw['strict_slashes'] = kw.get('strict_slashes', True)
>>     return app.route(*a, **kw)
>>
>> Then use @route instead of @app.route to get the behavior of
>> strict_slashes defaulting to True.
>>
>> -Steve
>>
>
>
> Yeah, adding a new decorator is cleaner than subclassing the whole app
> since it limits it's effects to just the new decorator. You could even use
> functools.partial:
>     route = functools.partial(app.route, strict_slashes=False)
>
> -andy
>



-- 
Sincerely,
Igor Davydenko

Re: [flask] Turn off strict_slashes on all routes

From:
Andy Wilson
Date:
2013-06-19 @ 19:54
You could subclass flask.Flask and override add_url_rule. Untested, but
something like: https://gist.github.com/wilsaj/5817469


-andy





On Wed, Jun 19, 2013 at 2:31 PM, Matthew Lewis <matt@mplewis.com> wrote:

> Hello,
>
> I've got a Flask script looking like this so far:
>
> @app.route('/')
> def index():
>     project_list = projects.get_projects()
>     return render_template('index.html', projects=project_list)
>
> @app.route('/blog', strict_slashes=False)
> def blog():
>     ... do things ...
>
> @app.route('/<project>', strict_slashes=False)
> def project(project):
>     ... do things ...
>
> We plan on adding a lot more routes and setting strict_slashes=False
> on all of them. Is there any way to set the default strict_slashes
> setting to False and override to True on specific routes?
>
> Thanks,
> Matt
>