Dexter Lakin
Dexter Lakin

Reputation: 68

django 2.0.4 urls not working

Django suddenly does not seem to be processing urls correctly. I followed part 3 of "Writing your first Django app" again with just a polls view and the urlsconf. It isn't working. What am I missing?

Here is my views.py:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

Here is my polls\urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

whyitsnotworking\urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

whyitsnotworking\settings.py:

INSTALLED_APPS = [
    'polls',

Here is the directory for my polls app: migrations admin.py apps.py models.py tests.py urls.py views.py init.py

I can run the test server:

Run 'python manage.py migrate' to apply them.
April 08, 2018 - 16:25:27
Django version 2.0.4, using settings 'whyitsnotworking.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[08/Apr/2018 16:25:33] "GET / HTTP/1.1" 200 16348
Not Found: /polls
[08/Apr/2018 16:25:39] "GET /polls HTTP/1.1" 404 1964

But get the following error message:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/polls

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

    admin/

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

Upvotes: 0

Views: 3464

Answers (2)

Dexter Lakin
Dexter Lakin

Reputation: 68

The issue was that I had the urls file in mysite\urls.py, not mysite\mysite\urls.py

What a dummy!

Upvotes: 0

Thomas Jiang
Thomas Jiang

Reputation: 1323

Remove the include() around admin.site.urls, because you're calling the direct pythonic path towards the urls module in admin.site, you don't need to use include(). You only need to use include() if you want to tell Django the path towards your urls.py file that you want to include, and that's usually a string that exists in formats similar to <appname>.urls.

Upvotes: 2

Related Questions