Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
/ urljects Public archive

Deprecated! (Django routing without urls.py files, inspired by Flask.)

License

Notifications You must be signed in to change notification settings

Visgean/urljects

Repository files navigation

URLjects - project no longer maintained

Since Django 2.0 introduced new system of routing I dont think kind of approach is desired anymore.

Travis CL Documentation Status Pypi Code Health Requirements Status Coverage Status

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 urls.py

With the use of include_view() you can avoid urls.py and include your app's views directly in root urls.py.

    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 name and 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 url_view decorator.

from urljects import url_view

@url_view(U / 'category' / rest)
def detail(request, rest)
    ...

After that you can user view_include instead of creating urls.py and then old-style 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 slug or rest as shown above inside your urls.py. We even provide modified url function to strip away the boilerplate of .as_view(),

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 DetailView.url_name. 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.