URLjects - project no longer maintained
Since Django 2.0 introduced new system of routing I dont think kind of approach is desired anymore.
Library which greatly simplifies django urls definition! And as a side effect it makes translated urls amazingly easy. Just compare
# old urls notation url('^detail/(?<slug>[\w-]+)', MyDetailView.as_view(), name='detail') # easified !even translated! notation url(U / _('detail') / slug, MyDetailView, name='detail')
Getting rid of
With the use of
include_view() you can avoid
urls.py and include
your app's views directly in root
from urljects import view_include # inside your root urls.py urlpatterns = [ # old style url("myapp/", include("myapp.urls")), # new urljects style url("myapp/", view_include("myapp.views")) ]
Soo how to put urls directly into views?
I am glad you asked! For class based views simply inherit from
URLView and add
url as their attributes.
from urljects import URLView, U, slug from django.views.generic import DetailView class ItemDetail(URLView, DetailView): name = 'detail' url = U / 'detail' / slug
A lot of people enjoy functional views, for those there is
from urljects import url_view @url_view(U / 'category' / rest) def detail(request, rest) ...
After that you can user
view_include instead of creating
include them afterwards.
I want to keep my urls.py
Quite often you need some
urls.py - for example your root urls. Then you can
use patterns like
rest as shown above inside your
We even provide modified
url function to strip away the boilerplate of
from urljects import U, slug, url url_patterns = ( url(U / 'detail' / slug, view=DetailView), # instead of url(r'^detail/(?P<slug>[\w-]+)' , view=DetailView.as_view(), name='detail'), )
The name of the view has been taken from
There are also some common regular patterns like slugs and UUIDs so that you
can focus on more important stuff than on debugging regular expressions.