What is the ideal format/structure of urlconfs in django 2

439 views Asked by At

I creating an application and one of its urlconf is as follows

urlpatterns = [
    path('', DashboardView.as_view(),name='dashboard:index'),
]

I am coming from PHP background (say Laravel) where we name our routes like below

  • dashboard:index - for get
  • dashboard:store - for post
  • dashboard:update - for patch etc...

So I named my route as above, but while performing the system check, the following warning comes up.

System check identified some issues:

WARNINGS: ?: (urls.W003) Your URL pattern '' [name='dashboard:index'] has a name including a ':'. Remove the colon, to avoid ambiguous namespace references.

System check identified 1 issue (0 silenced).

So my question is what is the ideal naming format of URLs in Django in general.

  • dashboard_index ?
  • dashboard.index ?
1

There are 1 answers

3
schrodingerscatcuriosity On

I guess the best place to find some kind of convention is in the django admin app, where I found this:

urlpatterns = [
    url(r'^$',
        views.BaseAdminDocsView.as_view(template_name='admin_doc/index.html'),
        name='django-admindocs-docroot'),
    url(r'^bookmarklets/$',
        views.BookmarkletsView.as_view(),
        name='django-admindocs-bookmarklets'),
    # and so on...
]

So, a string representing the url with dashes between words. I think is also important the name to be very explicit, not acronyms or shortened names.

EDIT:

Example of general url/path naming from the docs (2.1):

path('archive/', views.archive, name='news-archive')

Also it's a good idea to have in mind python code style.