161π
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'),
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'),
]
- [Django]-How to query Case-insensitive data in Django ORM?
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
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'),
]
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-How to implement followers/following in Django
- [Django]-How to get value from form field in django framework?
5π
url()
is deprecated in newer version of django. So instead of 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'),
]
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-How do I use a dictionary to update fields in Django models?
- [Django]-Whatβs the difference between a project and an app in Django world?
3π
If it doesnβt work add this code to yoursite\urls.py inside urlpatterns:
path('music/<int:album_id>/', views.detail, name="detail"),
- [Django]-What is the best AJAX library for Django?
- [Django]-Complete django DB reset
- [Django]-How to create a Django queryset filter comparing two date fields in the same model
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'),
]
- [Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
- [Django]-Favorite Django Tips & Features?
- [Django]-Models.py getting huge, what is the best way to break it up?
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),
]
- [Django]-Django: How do I add arbitrary html attributes to input fields on a form?
- [Django]-Django create userprofile if does not exist
- [Django]-How to concatenate strings in django templates?