All handlers must be a subclass of gunstar.http.RequestHandler
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler
class Handler(RequestHandler):
def get(self):
self.response.write('respond GET method')
def post(self):
self.response.write('respond POST method')
Subclasses of RequestHandler have:
Example:
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
name = self.request.GET.get('name', 'Stranger')
self.response.write('Hello, {0}'.format(name))
You need to set TEMPLATE_PATH in your config:
# -*- coding: utf-8 -*-
from gunstar.app import Application
from gunstar.http import RequestHandler
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
class ConfigSettings(object):
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
class IndexHandler(RequestHandler):
def get(self):
self.render_template('index.html', var1='var1', var2='var2')
routes = (
('/', IndexHandler, 'index'),
)
app = Application(routes=routes, config=ConfigSettings)
You can add filters and globals overriding methods:
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler
# filter
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
# global
def hello(name):
return 'Hello {0}'.format(name)
class BaseHandler(RequestHandler):
def get_template_globals(self):
template_globals = super(BaseHandler, self).get_template_globals()
template_globals['hello'] = hello
return template_globals
def get_template_filters(self):
template_filters = super(BaseHandler, self).get_template_filters()
template_filters['datetimeformat'] = datetimeformat
return template_filters
The method abort() is used to send a http code to client:
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.abort(404, message='Not found page.')
# shortcut for:
# self.response.status_code = 404
# self.seponse.write('Not found page.')
The method redirect() is used to redirect client to another location:
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
self.redirect('http://gunstar.readthedocs.org')
# Move to http://gunstar.readthedocs.org with http code = 302
class IndexHandler2(RequestHandler):
def get(self):
self.redirect('http://gunstar.readthedocs.org', permanent=True)
# Move to http://gunstar.readthedocs.org with http code = 301
class IndexHandler3(RequestHandler):
def get(self):
self.redirect('http://gunstar.readthedocs.org', status_code=307)
# Move to http://gunstar.readthedocs.org with http code = 307
The method reverse_route() is used to generates a url to the given route name:
# -*- coding: utf-8 -*-
from gunstar.app import Application
from gunstar.http import RequestHandler
class IndexHandler(RequestHandler):
def get(self):
post_url = self.reverse_route('post_detail', 'my-post-slug')
self.redirect(post_url)
class PostHandler(RequestHandler):
def get(self, post_slug):
self.response.write('This is the post {0}'.format(post_slug))
routes = (
('/', IndexHandler, 'index'),
('/posts/{post_slug:slug}/', PostHandler, 'post_detail'),
)
app = Application(routes=routes)