Ashish Kumar
Ashish Kumar

Reputation: 53

Django: cannot import name path

My urls.py looks like this:

urlpatterns = [
    path('',views.index, name='index'),
    path('entry/(<int:pk>)' , views.details,name='details'),
    path('admin/', admin.site.urls),
]

but when i try to run it i get error as cannot find path.

Attempt 1 :

I tried to use url instead but I am not sure how to use second line into url. This does not seems to work:

urlpatterns = [
    url(r'^$',views.index, name='index'),
    url(r'^entry/(?P<pk>\d+)/' , views.details,name='details'),
    url(r'^admin/', admin.site.urls),
]

Upvotes: 0

Views: 3943

Answers (2)

Lim
Lim

Reputation: 779

I wanted to comment on Monhammand's answer. But, I couldn't do that, because I needed to have at least 50 reputations. So, I submit this as an answer.

If you would like to use regular expressions in Django 2.X, you can use re_path().

https://docs.djangoproject.com/en/2.0/ref/urls/#re-path

urlpatterns = [
  re_path(r'^$',views.index, name='index'),
  re_path(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
  re_path(r'^admin/', admin.site.urls),
]

Upvotes: 1

Ali
Ali

Reputation: 2591

If you are using django 2.x, do like this:

urlpatterns = [
    path('',views.index, name='index'),
    path('entry/<int:pk>/' , views.details,name='details'),
    path('admin/', admin.site.urls),
]

If you are using django 1.x, do like this:

urlpatterns = [
    url(r'^$',views.index, name='index'),
    url(r'^entry/(?P<pk>\d+)/$' , views.details,name='details'),
    url(r'^admin/', admin.site.urls),
]

/ and $ are important

Upvotes: 0

Related Questions