Reputation: 11
i am cuurently working on django poll app tutorial and i am getting this error .
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/
admin/
The empty path didn't match any of these.
already tried restarting the server ,migrate the data and settings.py having installed apps . however django tutorial doesnot ask you for this until now. working on windows 10 64 bit
polls/urls.py
from django.urls import path
from . import views
urlpatterns=[path('',views.index,name='index'),]
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')),]
results should be response from the views.py file
Upvotes: 1
Views: 2167
Reputation: 51
I had this problem when I put the urls.py by mistake in the upper mysite/ upper root folder, to solve it just move the urls.py file inside mysite/mysite/ path as the root folder structure is like this:
mysite/ (don't put urls.py inside this one!)
manage.py
mysite/ (put it inside here!)
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
please check this answer as it looks like a duplicate of the same problem
404 Error in "Writing your first Django App" tutorial
Upvotes: 3
Reputation: 1398
Your code is ok , you are trying to access a url that you never mentioned in the mysite/urls.py.
Change url pattern in msysite/urls.py to the following to access the polls app 127.0.0.1:8000/
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('polls.urls')),
]
And in polls/urls.py
urlpatterns = [
path('', views.index, name='index'),
]
NOTE: The urlpattern you tried will work at http://127.0.0.1:8000/polls/ Since you used following code in mysite/urls.py
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls),
path('polls/',include('polls.urls')),
]
Upvotes: 0
Reputation: 623
I think everything with your code is ok. But you are propapbly visiting "localhost:8000" instead of "localhost:8000/polls".
If you want to work on polls app on root URL, you need to edit your code this way:
mysite/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')),
]
polls/urls.py
urlpatterns = [
path('', views.index, name='index')),
]
I think also you have to make your code more clear, too look more readable for everyone (for you too!)
Upvotes: 0