Usman Gill
Usman Gill

Reputation: 31

django URLconf current path didn't match

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
admin/

The current path, polls/, didn't match any of these.

My code is... from (app)polls urls.py

from django.urls import path
from . import views


urlpatterns = [
    path('', views.index, name='index'),
]

from (project)mysite urls.py

from django.urls import include, path
from django.contrib import admin


urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),

]

from views.py project file

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello world, you are at the polls index.")

Upvotes: 0

Views: 6452

Answers (3)

ying
ying

Reputation: 11

I am a beginner and I have been troubled for a long time with the same problem. The key to the problem is the directory:

mysite/
    mysite/
    polls/
    ....

I suspect that you have created a new file(urls.py) in mysite(The outermost layer of folders) directory and it's invalid. You should modify the file: mysite/mysite/urls.py

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

Upvotes: 1

R.R.C.
R.R.C.

Reputation: 6711

If you want your view index as a home view, the first page to be rendered on the address localhost:port/.

Then,

In polls.urls.py should be (the same as yours):

...
path('', views.index, name='index'),
...

And in project.urls.py

...
path('', include('polls.urls')),
...

It should work.

An extra. Now if you want to render on the address, lets say, localhost:port/polls

Then your path pattern inside you app.url, (not project.url), would be:

path('polls/', polls_view, name='polls')

Hope it helps.

Upvotes: 2

Ritesh Agrawal
Ritesh Agrawal

Reputation: 821

Since you are accessing localhost:8000, the search path is empty string '' and in your project urls, there are only two pattern for polls and admin. That's why you are getting the error. So you need to access localhost:8000/polls to get the result from index view of polls app.

Upvotes: 0

Related Questions