Django url path migration (1.11->3.1) warning for route symbols

1.1k Views Asked by At

I'ved got this warning in Django 3.1.5 when doing the migration from 1.11

?: (2_0.W001) Your URL pattern 'employee/attendanceactivity/attendance/(?P<attendance_id>\d+)/' [name='employee-attendanceactivity-detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().

here is the statement from url.py

path('employee/attendanceactivity/attendance/(?P<attendance_id>\d+)/', 
    views.employee_attendanceactivity_detail, name = 'employee-attendanceactivity-detail'), 

How should I fix this warning ? Thanks

2

There are 2 best solutions below

4
Awin On BEST ANSWER

From Django 2.0 onwards, re_path should be used for matching regular expressions in URL patterns.

Modify your path as follows:

from django.urls import re_path

urlpatterns = [
    # ...
    re_path(r'^employee/attendanceactivity/attendance/(?P<attendance_id>\d+)/$', views.employee_attendanceactivity_detail, name = 'employee-attendanceactivity-detail'), 
    # ...
]

ref. Django URL dispatcher documentation.

0
John On

Stack won't let me comment, unfortunately, but the answer to your follow-on question is that the ^ at the start of the pattern is a regex "start-of-line" anchor. While your path will likely work without it, it's generally a good idea to include ^ at the start and $ at the end of your re_paths.