Home > Mobile >  Django 404 Error, views not found. Why? View is not show up or is not found [SOLVED]
Django 404 Error, views not found. Why? View is not show up or is not found [SOLVED]

Time:05-24

I programmed in Ruby beforehand and am now moving to Django. I am trying to follow the article here. https://docs.djangoproject.com/en/4.0/intro/tutorial01/

#polls/url.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
# mysite /urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
  • Here is my tree: polls/ --> init.py -> admin.py -> apps.py -> migrations/ --> init.py -> models.py -> tests.py -> urls.py -> views.py ->


Of course, I ran django-admin startproject mysite before all of tihs and the output for the version is:

└──╼ $python -m django --version
4.0.4

I tried starting the server:

─╼ $python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 24, 2022 - 13:47:13
Django version 4.0.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

... but when I click the link I get this error:


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

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.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Of which is a common 404 error. Why is this the case? I ran everything as instructed. Maybe there is something wrong with the sqlite server? I am very lost and any help will be appreciated,

CodePudding user response:

You don't have any root route. So adding

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

should help or try to open http://127.0.0.1:8000/polls/ or http://127.0.0.1:8000/admin/

CodePudding user response:

Your polls/url.py is based on the path 'polls/' (defined on mysite/urls.py)

Actually you only have two paths defined :

  • polls/
  • admin

  • If you add in polls/url.py

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

    You will also have the path polls/index defined

    • Related