3

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

Axil
  • 3,606
  • 10
  • 62
  • 136

2 Answers2

8

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.

Awin
  • 884
  • 7
  • 9
  • you didnt had ^ ? like this re_path(r'^.... what is the difference omitting ^ ? @Awin – Axil Apr 24 '21 at 03:29
  • `^` means start of string and `$` is the end of string. Without them it will match the `re_path` even in between strings. Updated the answer to include both ^ and & – Awin Apr 26 '21 at 06:24
  • what if I have something like this, do I put $ at the end also --> re_path(r'^order/list/view_order/(?P\d+)', orderadmin_views.orderadmin_view_order, name='orderadmin_view_order'), – Axil Apr 28 '21 at 06:04
  • Yes, adding $ at the end results in a more specific regex. In your case, without a $, it will also match `order/list/view_order/123/some-string`. – Awin May 03 '21 at 13:30
0

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.

John
  • 46
  • 2