[Flask] Having troubles with template rendering in class-based routing

Don Dwiggins ddwiggins at advpubtech.com
Thu Dec 3 13:07:57 EST 2015


OK, problem solved.  in thrashing around, I added "defaults={'name': 
None}" to add_url_rule; this was causing the value in the URL to be ignored.

A similar case is described at 
http://stackoverflow.com/questions/15421193/using-defaults-with-app-add-url-rule-in-flask. 
The lesson seems to be this: if you have a rule for "/Foo/<bar>", it 
won't match with a URL lacking a value to match "bar".  To handle that 
case, you need to register another rule for "/Foo/", with a "defaults" 
parameter to give "bar" a value.

Unfortunately, this isn't made very clear in either the Flask or 
Werkzeug documentation.

On 11/25/15 12:56 PM, Don Dwiggins wrote:
> I'm having trouble doing routing with class-based dispatching.
>
> In my url_map, I have a rule: "<Rule '/Home/<name>' (HEAD, OPTIONS, 
> GET) -> Home>".
>
> The view_func set to:
>> self.RenderTemplateView.as_view( self.label, self.label+'.html', 
>> 'templates')
> where self.label='Home', and there's a template in 
> "templates/Home.html" that renders differently depending on whether 
> <name> has a value or is None.
>
> RenderTemplateView is defined as:
>>     class RenderTemplateView(flask.views.View):
>>         def __init__(self, template_name, *args, **kwargs):
>>             self.template_name = template_name
>>
>>         def dispatch_request(self, *args, **kwargs):
>>             return flask.render_template(self.template_name)
>
> Call it with "http://127.0.0.1:5000/Home/thing" renders the template, 
> except that the <name> variable is bound to None, not to "thing".  If 
> I add "name='thing' " to the render_template call, it does get 
> included in the display.
>
> I've tried a number of things, including inserting a line "x = 1/0" 
> before the render_template call to get a traceback, then examining the 
> sequence of calls; I couldn't find anything helpful.
>
> Decorator-based routing works fine, but isn't an option for me.
>
> Any good words appreciated,
>
>
> -- 
> Don Dwiggins
> Advanced Publishing Technology
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20151203/bc35f278/attachment.html>


More information about the Flask mailing list