Django 2.0 path error ?: (2_0.W001) has a route that contains '(? P <', starts with the character '^' or ends with the character '$'
I am new to Django and trying to create internal code for a music application on my website.
I created the correct view in my views.py file (in the correct directory) as shown below:
def detail(request, album_id): return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>") however, when creating a URL or path for this (shown below)
#/music/71/ (pk) path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'), A warning appears on my terminal that:
?: (2_0.W001) Your URL pattern '^(?P<album_id>[0-9])/$' [name='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(). and whenever /music/ (for which the path works) is followed by a number such as /music/1 (which I want to do), the page cannot be found, and the terminal displays the above warning.
It might be a simple mistake, and I'm just stupid, but I'm new to Django expressions and Python regular expressions, so any help is appreciated.
The new path() syntax in Django 2.0 does not use regular expressions. You want something like:
path('<int:album_id>/', views.detail, name='detail'), If you want to use regex, you can use re_path() .
re_path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'), The old url() still works and is now an alias of re_path , but most likely it will be deprecated in the future.
url(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'), Just to add to what @alasdair mentioned, I added re_path as part of the include, and it works great. Here is an example
Add re_path to your import (for django 2.0)
from django.urls import path, re_path urlpatterns = [ path('admin/', admin.site.urls), re_path(r'^$', home, name='home'), ]