Django Tutorial: Inexplicable 404 error after completing the first page

I ended up https://docs.djangoproject.com/en/1.9/intro/tutorial01/

Intended behavior works. My survey index is shown. But there is one unintended consequence that I do not understand. When I go to localhost:8000, I get a page not found. What for?

This is mine mysite/mysite/urls.py, as the textbook explained.

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

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

The server says:

Not Found: /
[11/Feb/2016 04:25:46] "GET / HTTP/1.1" 404 2010

When I delete the poll line, 404 disappears. I.e:.

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

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

Now the server says:

Not Found: /
[11/Feb/2016 04:24:23] "GET / HTTP/1.1" 200 1767

So, I think this is some kind of default error, but I still don't quite understand if I will make a mistake or if this is a defined behavior. My other code is the same as in the tutorial code snippets.

+4
2

, , , polls. URL-, - http://localhost:8080/.

mysite/urls.py:

from polls import views

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

polls .

+1

! .

URL-, Django ( ). .

URL , django Not found: {url}, runserver. 404, .

debug . django/views/debug.py:

def technical_404_response(request, exception):
    # some extra code here
    if (not tried                           # empty URLconf
        or (request.path == '/'
            and len(tried) == 1             # default URLconf
            and len(tried[0]) == 1
            and getattr(tried[0][0], 'app_name', '') == getattr(tried[0][0], 'namespace', '') == 'admin')):
        return default_urlconf(request)
    # more extra code here

Django , , URL- . , default_urlconf. :

  • URL
  • URL- '/', URL, admin app

, , URL , Django default_urlconf. URL admin, URL-. - :

Not Found: /random/url/
[11/Feb/2016 04:24:23] "GET /random/url/ HTTP/1.1" 200 1767

default_urlconf:

def default_urlconf(request):
    "Create an empty URLconf 404 error response."
    t = DEBUG_ENGINE.from_string(DEFAULT_URLCONF_TEMPLATE)
    c = Context({
        "title": _("Welcome to Django"),
        "heading": _("It worked!"),
        "subheading": _("Congratulations on your first Django-powered page."),
        "instructions": _("Of course, you haven't actually done any work yet. "
            "Next, start your first app by running <code>python manage.py startapp [app_label]</code>."),
        "explanation": _("You're seeing this message because you have <code>DEBUG = True</code> in your "
            "Django settings file and you haven't configured any URLs. Get to work!"),
    })

    return HttpResponse(t.render(c), content_type='text/html')

( HTTP- HttpResponse = > 200)

+4

Source: https://habr.com/ru/post/1628239/


All Articles