why is Django giving me a 404 error

人盡茶涼 提交于 2021-01-29 10:30:08

问题


I am currently trying to complete the Django 2.0 tutorial Poll application. I am using Django version 2.0.7 and Python 3.7.0. I have set up a virtual environment for this project. I am very new to Django so apologies if my terminology is incorrect.

Basically, I cant get part 1 to work.

I have tried numerous times to run it and I cant seem to figure out why it wont work.

Before I create the Polls app the site runs correctly and I get the rocket ship, however after I create the app, update polls/views.py, create the polls/urls.py file and update the mysite/urls.py, I get the following 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:

  1. polls/
  2. admin/

The empty path didn't match any of these.

I thought my code was incorrect, so I copied and pasted the code from the website directly and tried to run it. It still wont work.

Below is the code I am trying and the directory layout. Any help would be greatly appreciated. I'm sure I am just missing something simple.

Thank you.

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),
]

polls/urls.py

from django.urls import path

from . import views

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

polls/views.py

from django.http import HttpResponse


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

Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite

print.txt

settings.py

urls.py

wsgi.py

init.py

pycache

Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls

admin.py

apps.py

migrations

models.py

tests.py

urls.py

views.py

init.py

pycache


回答1:


have you added your app name inside settings.py make sure you have added the name in settings.py which resides under project folder

in your case it will be here:

Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite

like this:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',] # <--- polls in your case

make sure you are using your routes you have defined to access the correct page

for example in your case:

your website's routes are:

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

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

and your application 'polls' routes are:

from django.urls import path

from . import views

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

according to your views.py file

which is this:

from django.http import HttpResponse

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

you must visit http://localhost:{port number}/polls/ or http://127.0.0.1:{port number}/polls/

default port number is 8000

after running server using python manage.py runserver

to get your page as an output on browser



来源:https://stackoverflow.com/questions/51347587/why-is-django-giving-me-a-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!