Base views

Views

View

class flask_views.base.View

View which will dispatch requests based on request method.

This class inherits from:

  • flask.views.MethodView

For example, a GET request will be dispatched to the get method, a POST request will be dispatched to the post method. Example usage:

class MethodView(View):

    def get(self, *args, **kwargs):
        return 'Hello {0}'.format(self.kwargs.get('user'))

When you have a URL route '/<user>/' and you GET /john/, it will return 'Hello john'.

dispatch_request(*args, **kwargs)

Dispatch the request based on HTTP method.

This sets the arguments and keyword-arguments passed by the URL route dispatcher to self.args and self.kwargs, then it will dispatch the request to the right method.

TemplateView

class flask_views.base.TemplateView

View class for rendering templates.

This class inherits from:

Example usage:

class IndexTemplateView(TemplateView):
    template_name = 'index.html'
get(*args, **kwargs)

Render the template on request.

The keyword-arguments passed by the URL dispatcher are added to the context data.

Returns:Output of TemplateResponseMixin.render_to_response().
get_context_data(**kwargs)

Get context data for rendering the template.

Returns:A dict containing the following keys:
params
A dict containing the kwargs.

Mixins

TemplateResponseMixin

class flask_views.base.TemplateResponseMixin

Mixin class for rendering templates.

This will use the flask.render_template() method for rendering (Jinja2) templates.

render_to_response(context_data={})

Render template with the given context data.

Parameters:context_data – A dict containing the context data. Optional.
Returns:The rendered template as a string.
template_name = None

Set this variable to the template you want to render.

Table Of Contents

Related Topics

This Page