89

I'm trying to create the back-end code for a music application on my website.

I have 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 the URL or path for this (shown below)

#/music/71/ (pk)
path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),

I am experiencing a warning on my terminal stating:

?: (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 the /music/ (for which the path works) is followed by a number, such as /music/1 (which is what I want to be able to do) the page cannot be found and the terminal gives the above warning.

Joe Tynan
  • 895
  • 1
  • 6
  • 5
  • I think you either want to use [`re_path`](https://docs.djangoproject.com/en/2.0/ref/urls/#re-path) or don't use regex in the url pattern (`path('music//', ....)`. – ayhan Dec 05 '17 at 19:54
  • You are mixing new and old urls syntax. Either change url to `path('/', views.detail, name='detail')` or use [`re_path`](https://docs.djangoproject.com/en/2.0/topics/http/urls/#using-regular-expressions) – Borut Dec 05 '17 at 19:55
  • 1
    It would be so nice if django would tell what to use instead. – Soerendip Oct 15 '18 at 21:04

7 Answers7

158

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 a regular expression, 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 to re_path, but it is likely to be deprecated in future.

url(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thanks this worked perfectly, I figured I was using url() instead of path() and trying to merge the two together without realising which obviously doesn't work. – Joe Tynan Dec 05 '17 at 20:08
23

Just to add to what @alasdair mentioned, I added re_path as part of the include and it works fine. 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'),

]
Stryker
  • 5,732
  • 1
  • 57
  • 70
9

Instead of using 're_path' you can also use ''(empty string) as the first argument of your path(). I have used it and it worked for me.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),
]
Akash Gupta
  • 91
  • 1
  • 2
4

url() is deprecated in newer version of django. So instead of using url use re_path() in your urls file as follows:

from django.urls import path, re_path
from . import views

urlpatterns = [
    #url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
    path('', views.index, name='index'),
    re_path(r'^(?P<album_id>[0-9])/$', views.detail, name='detail'),
]
P S
  • 435
  • 1
  • 8
  • 26
3

If it doesn't work add this code to yoursite\urls.py inside urlpatterns:

path('music/<int:album_id>/', views.detail, name="detail"),
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hamza Naji
  • 51
  • 1
2

Use an empty string '' instead of '/' or r'^$'. It works like a charm. Code is as below:

from django.urls import path, re_path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name='home'),
]
DILIP
  • 21
  • 1
  • 3
0

In django 2.0 version primary key write this way...

urls.py

from django.urls import path

from . import views


urlpatterns = [
    path('', views.course_list),
    path('<int:pk>/', views.course_detail),
]