Why is my Django3 project created with regex code?

Viewed 101

I am very new to Django.

I am using Django 3 and when I create a new Django project, the urls.py file has this code:

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
   url(r'^admin/', admin.site.urls),
]

I thought this regex code was for older versions of Django. The newer Django 3 should use path.

Am I doing anything incorrectly?

1 Answers

Short answer: modern django-admin uses path.

You do not per se needs to user a path, since not all regexes map to the builtin path converters. But you should use re_path [Django-doc], since url [Django-doc] is, as specified in the documentation:

This function is an alias to django.urls.re_path(). It’s likely to be deprecated in a future release.

 

I thought this regex code is for older version of Django. The newer Django 3 should use path.

Since , one can make use of path, and thus use path converters. But if the pattern is not a builtin parth converter, it can be rather hard to introduce it yourself, and perhaps not worth the effort if you need it only once. Therefore you still might want to use re_path.

Note that the program that creates the project is django-admin. If I run this with django-tools-3.0.5, I get the expected path:

$ django-admin --version
3.0.5
$ django-admin startproject django_test
$ cat django_test/django_test/urls.py
"""django_test URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

so perhaps you forgot to update the django-admin tooling.

Related