Skip to content

Routing urls.py

Kyle J. Roux edited this page Sep 9, 2015 · 2 revisions

Routing

To handle routing we use a little tip from Django and we collect our routes for each app in a urls.py file. In this file we will import the apps views, and add them to a group of tuples named routes. To add a route add a tuple with (route_url,endpoint(optional if not using cbv's),view_func/class) Here is an example using class based views:

from .views import MainView, SubView
from . import app_blueprint

routes = (
    (app_blueprint,),
         ('/','main',MainView),
         ('/sub','sub',SubView),
)

Here is the same example using simple functions instead of classes:

from .views import main_view, sub_view
from . import app_blueprint

routes = (
    (app_blueprint,),
         ('/',main_view),
         ('/sub',sub_view),
)

When using view functions, the endpoint will be the functions name, we must supply the endpoint to view classes, because it is standard to subclass flask.views.MethodView and its methods correspond to request methods, so we must give their endpoints names.

Clone this wiki locally