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()
|
Bases: flask_velox.mixins.context.ContextMixin, flask_velox.mixins.template.TemplateMixin
Renders a template with optionl context.
Relative template path, e.g: templates/home.html
Default context to use when rendering the template
Example
1 2 3 4 5 | class HomeView(TemplateView):
template = 'templates/home.html'
context = {
'hello': 'word'
}
|
x.__delattr__(‘name’) <==> del x.name
default object formatter
x.__getattribute__(‘name’) <==> x.name
x.__hash__() <==> hash(x)
helper for pickle
helper for pickle
x.__repr__() <==> repr(x)
x.__setattr__(‘name’, value) <==> x.name = value
size of object in memory, in bytes
x.__str__() <==> str(x)
Adds a new element to the context.
Parameters: |
|
---|---|
Returns: | The new context |
Return type: | dict |
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.
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 |
Handle HTTP GET requets using Flask MethodView rendering a single html template.
Returns: | Rendered template |
---|---|
Return type: | str |
Propety method which returns the current context.
Returns: | Current context value |
---|---|
Return type: | dict |
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 |
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 |
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
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 |