flask_velox.views.template

Module provides classes for rendering templates using Flask MethodView.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from flask import Flask
from flask.ext.velox.views.template import TemplateView
from flask.views import MethodView

app = Flask(__name__)

class HomeView(TemplateView):
    template = 'templates/home.html'
    context = {
        'hello': 'word'
    }

app.add_url_rule('/', view_func=HomeView.as_view('home'))

app.run()
class flask_velox.views.template.TemplateView(*args, **kwargs)

Bases: flask_velox.mixins.context.ContextMixin, flask_velox.mixins.template.TemplateMixin

Renders a template with optionl context.

template str

Relative template path, e.g: templates/home.html

context dict, optional

Default context to use when rendering the template

Example

1
2
3
4
5
class HomeView(TemplateView):
    template = 'templates/home.html'
    context = {
        'hello': 'word'
    }
__delattr__

x.__delattr__(‘name’) <==> del x.name

__format__()

default object formatter

__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__sizeof__() → int

size of object in memory, in bytes

__str__

x.__str__() <==> str(x)

add_context(key, val)

Adds a new element to the context.

Parameters:
  • key (str) – The context key name
  • val – The value of the new context item of any type
Returns:

The new context

Return type:

dict

classmethod as_view(name, *class_args, **class_kwargs)

Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request() method on it.

The arguments passed to as_view() are forwarded to the constructor of the class.

del_context(key)

Removes an element from the context dictionary.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters:key (str) – The context key name
Returns:Success or Failure
Return type:bool
Returns:The new context
Return type:dict
get(*args, **kwargs)

Handle HTTP GET requets using Flask MethodView rendering a single html template.

Returns:Rendered template
Return type:str
get_context()

Propety method which returns the current context.

Returns:Current context value
Return type:dict
merge_context(subject)

Merge the passed dictionary into the current _context

Parameters:subject (dict) – The dict to merge into _context

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
    'hello': 'world',
    'foo': 'bar'
}
Returns:The new context
Return type:dict
render()

Renders a template. This method will attempt to pass context to the template but if the context attribute does not exist then an empty dict will be passed to the render_template method.

Returns:Rendered template
Return type:str
set_context()

A method which should be overridden when required to set extra context on a per view basis. This method is not required to be implemented however is the recommended way of setting extra context

update_context(new)

Overwrites the existing context with the provided new context value.

Example

>>> class FooView(ContextMixin):
...     context = {
...         'foo': 'bar',
...     }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
    'hello': 'world'
}
Parameters:new (dict) – Value to update context too
Returns:The new context
Return type:dict

http://thisissoon.com

Related Topics

This Page