hurl

Django Happy Urls
Download

hurl Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Tomek Paczkowski & Aleksandra Sendecka
  • Publisher web site:
  • https://github.com/oinopion/

hurl Tags


hurl Description

Django has nice routing, but it's too low level. Regexps are powerful, but but have cryptic syntax. hurl is a Python library that strives to make writing DRY URLs a breeze.Consider a standard urls.py:urlpatterns = patterns('blog.entries.views', url(r'^$', 'recent_entries', name='entries_recent_entries'), url(r'^(?P< entry_slug >+)/$', 'show_entry', name='entries_show_entry'), url(r'^(?P< entry_slug >+)/new/$', 'new_entry', name='entries_new_entry'), url(r'^(?P< entry_slug >+)/edit/$', 'edit_entry', name='entries_edit_entry'), url(r'^(?P< entry_slug >+)/delete/$', 'delete_entry', name='entries_delete_entry'), url(r'^(?P< entry_slug >+)/comments/$', 'comments_list', name='entries_comments_list'), url(r'^(?P< entry_slug >+)/comments/(\d+)/$', 'comment_details', name='entries_comment_detail'),)It has many issues:- you need to remember about the '^' and the '$'- you repeat the entry_slug url- you need to remember arcane named group syntax- you repeat the + group- you associate name with urls confBetter way of writing urls would be:urlpatterns = hurl.patterns('blog.entries.views', { '': 'recent_entries', '< entry_slug:str >': { '': 'show_entry', 'new': 'new_entry', 'edit': 'edit_entry', 'delete': 'delete_entry', 'comments': 'comments_list', 'comments/< :int >': 'comment_detail', }),)It conveys url structure more clearly, is much more readable and avoids repetition.More examplesDjango tutorial:# original:urlpatterns = patterns('', (r'^articles/2003/$', 'news.views.special_case_2003', {}, 'news_special_case_2003'), (r'^articles/(?P< year >\d{4})/$', 'news.views.year_archive', {}, 'news_year_archive'), (r'^articles/(?P< year >\d{4})/(?P< month >\d{2})/$', 'news.views.month_archive', {}, 'news_month_archive'), (r'^articles/(?P< year >\d{4})/(?P< month >\d{2})/(?P< day >\d{2})/$', 'news.views.article_detail', {}, 'news_article_detail'),)# hurled:hurl = Hurl(name_prefix='news')hurl.matchers = r'\d{4}'hurl.matchers = r'\d{2}'hurl.matchers = r'\d{2}'urlpatterns = hurl.patterns('news.views', { 'articles': { '2003': 'special_case_2003', '< year >': 'year_archive', '< year >/< month >': 'month_archive', '< year >/< month >/< day >': 'article_detail', }})Product's homepage


hurl Related Software