Differences between revisions 6 and 17 (spanning 11 versions)
Revision 6 as of 2014-04-21 14:59:02
Size: 3064
Comment: Add RegexURLMap example
Revision 17 as of 2014-09-17 17:07:46
Size: 3464
Editor: techtonik
Comment: add Flask to the comparison
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
'''Routing''' is mapping URLs to code that handles them. '''Routing''' (also known as `request routing` or `URL dispatching`) is mapping URLs to code that handles them.
Line 30: Line 30:
==== Django Routing ==== ==== Flask Routing ====
{{{
@app.route('/hello/<username>')
def show_user_message(username):
    return 'Hello %s' % username
}}}

Get overview of URLs handled: search for ".route("

http://flask.pocoo.org/docs/0.10/quickstart/#routing

==== Django Routing Classic (pre 1.8 era) ====
Line 33: Line 44:
urlpatterns = patterns('', from django.conf.urls import patterns, url

urlpatterns = patterns('', # <-- common prefix for sugar-coating, like 'news.views'
Line 45: Line 58:
==== Improved Django Routing (Django-hotsauce) ==== ==== Django Routing (1.8+) ====
Line 47: Line 60:
[[EtienneRobillard/DjangoHotSauce|RegexURLMap]] provides a convenient interface to the Django routing system, allowing to make logical groups of urls: Classic routing scheme described above for historical reference [[https://docs.djangoproject.com/en/dev/releases/1.8/#django-conf-urls-patterns|is deprecated]]. Implicit imports that reference modules as strings break static analysis of Python code, empty first parameter is confusing to new users, so the new scheme is:
Line 51: Line 64:
from notmm.utils.urlmap import RegexURLMap, url from django.conf.urls import url
import news.views
Line 53: Line 67:
# init the constructor
urlpatterns = RegexURLMap()

# permanent routes
urlpatterns.add_routes('mainapp.custom.views',
        url(r'^$|index.html$', 'render_i18n_template', dict(extra_context={
            'blogentry_set': blogentry_set,
            #'recent_changes': recent_changes
            })),
)

# insert more routes from another app
urlpatterns.include('blogengine.contrib.userprofile.urls', prefix="^blog/")

# finish
urlpatterns.commit()
urlpatterns = [
    url(r'^articles/2003/$', news.views.special_case_2003),
    url(r'^articles/(\d{4})/$', news.views.year_archive),
    url(r'^articles/(\d{4})/(\d{2})/$', news.views.month_archive),
    url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', news.views.article_detail),
]

Routing (also known as request routing or URL dispatching) is mapping URLs to code that handles them.

Why routing is important? By directly connecting the content that you see on a web page (especially if it is an error), good routing drastically reduces time (and money) that required to improve this content (or fix the error). That's why Django has this link on its front page.

Ever heard of spaghetti code? Web applications is a good example of that. When we start reading a book - we have only single entrypoint - start of the book. If you read source code of web applications - there are usually no defined entrypoints, so the first thing you do is find some webpage and the run search to find out where is the code for it. Clearly defined routing not only saves you time on searching, but URLs gathered in one place give a good overview of application capabilities.

Good routing map is a like a map of the city you're going to explore.

Routing in Python Web Frameworks

Usability of routing component can be estimated with the help of the following questions:

  1. How easy is to get overview of all URLs that web application processes?
  2. How easy is to make reverse mapping (code to URL)?
  3. How easy is to serve static content URLs (css, js, images) by external server

Bottle Routing

@route('/hello/<name>')
def index(name):
  return '<b>Hello {{name}}</b>!'

Get overview of URLs handled: search for "@route"

http://bottlepy.org/docs/dev/index.html

Flask Routing

@app.route('/hello/<username>')
def show_user_message(username):
    return 'Hello %s' % username

Get overview of URLs handled: search for ".route("

http://flask.pocoo.org/docs/0.10/quickstart/#routing

Django Routing Classic (pre 1.8 era)

# --- urls.py ---
from django.conf.urls import patterns, url

urlpatterns = patterns('',  # <-- common prefix for sugar-coating, like 'news.views'
    url(r'^articles/2003/$', 'news.views.special_case_2003'),
    url(r'^articles/(\d{4})/$', 'news.views.year_archive'),
    url(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
    url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)

Get overview of URLs handled: find settings.py, look for ROOT_URLCONF for name of Python modules with the location of URL map (usually named 'urls'), find referenced module ('urls.py'), read urls.py to understand how urlpatterns is constructed (it may contain included apps).

https://docs.djangoproject.com/en/1.6/topics/http/urls/

Django Routing (1.8+)

Classic routing scheme described above for historical reference is deprecated. Implicit imports that reference modules as strings break static analysis of Python code, empty first parameter is confusing to new users, so the new scheme is:

# --- urls.py ---
from django.conf.urls import url
import news.views

urlpatterns = [
    url(r'^articles/2003/$', news.views.special_case_2003),
    url(r'^articles/(\d{4})/$', news.views.year_archive),
    url(r'^articles/(\d{4})/(\d{2})/$', news.views.month_archive),
    url(r'^articles/(\d{4})/(\d{2})/(\d+)/$', news.views.article_detail),
]

Routing (last edited 2014-09-17 17:10:46 by techtonik)

Unable to edit the page? See the FrontPage for instructions.