polls app Page not found (404)

Hello I am try to follow the official tutorial of django and try to add the page /polls . I am a totally beguinner on django .
Could you please help me ?

mysite/
urls.py

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

urlpatterns = [
path(‘’, include(‘pages.urls’)),
path(‘polls/’, include(‘polls.urls’)),
path(‘admin/’, admin.site.urls),
path(‘’, lambda request: HttpResponse(‘the cow jumped over the moon’)),
]

These are my files:

mysite/mysite/

settings.py

from pathlib import Path

Build paths inside the project like this: BASE_DIR / ‘subdir’.

BASE_DIR = Path(file).resolve().parent.parent

SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = ‘django-insecure-5@od4ay%v5eatrbi(ezo2fr0jmswemsmx!1w(399#f!e6cbni7’

SECURITY WARNING: don’t run with debug turned on in production!

DEBUG = True

ALLOWED_HOSTS =

Application definition

INSTALLED_APPS = [
‘polls.apps.PollsConfig’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
]

MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]

ROOT_URLCONF = ‘mysite.urls’

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: ,
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

WSGI_APPLICATION = ‘mysite.wsgi.application’

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: BASE_DIR / ‘db.sqlite3’,
}
}

AUTH_PASSWORD_VALIDATORS = [
{
‘NAME’: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.MinimumLengthValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.CommonPasswordValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.NumericPasswordValidator’,
},
]

LANGUAGE_CODE = ‘en-us’

TIME_ZONE = ‘UTC’

USE_I18N = True

USE_TZ = True

DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’

views.py

from django.shortcuts import render

Create your views here.

from django.http import HttpResponse

def index(request):
return HttpResponse(“Hello, world. You’re at the polls index.”)

First, a side note - when posting code or errors here, surround each file or traceback with lines of three backtick - ` characters. this means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

Notice that you entered a url without the trailing slash, while the path definition in your urls.py file has the trailing slash.

Also, what are the contents of the urls.py file in your polls directory?

mysite/polls/urls.py

sorry for the missing part and the bad presentation. I wanted to modify my post but when I tried it was not possible. I tried again in this answer to paste my code but it didn’t work neither that is why i gave you the screenshot.

Do you need more information ?
Thank you in advance for your help.

Can we see your project lvl urls.py file?

what does mean lvl ?

Level. Sorry :person_shrugging:
The urls.py file in your project. Not the one in the app folder

Thank you to answer to my questions :slight_smile:

This one ?

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

urlpatterns = [
path(‘’, include(‘pages.urls’)),
path(‘polls/’, include(‘polls.urls’)),
path(‘admin/’, admin.site.urls),
path(‘’, lambda request: HttpResponse(‘the cow jumped over the moon’)),
]

I had a similar problem once. Clearing the browser data, cookies and such worked.

Did you try entering the url with the trailing slash?

And what is the content of the polls/apps.py file?

Please do not post images of code here. They’re not readable on all devices, and they cannot be searched or quoted for future reference. When posting code or errors here, surround each file or traceback with lines of three backtick - characters. this means you’ll have a line of \``, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. Note: The lines of ``` before and after the code must be lines on their own.
Example:

# The line above this is ```
def some_function():
    return True
# The line after this is ```

Do you mean type that as an url ?
http://127.0.0.1:8000/polls/

polls/apps.py

from django.apps import AppConfig

class PollsConfig(AppConfig):
default_auto_field = ‘django.db.models.BigAutoField’
name = ‘polls’

Yes, did you try that?

Yes I did.
I also removed all my cookies.

How are you running your application? (Are you using runserver?)

It looks like you might be running this on Windows, is that correct?

Are you restarting the server after making your changes?

I use a window
I type on anaconda prompt

python manage.py runserver

sometimes

python manage.py migrate
python manage.py runserver

Are you stopping and restarting runserver every time you make changes to your code? Or are you verifying that runserver is recognizing that the code has been changed and is restarting itself?

I am stopping and restarting runserver every time i make changes to my code with ctr+c and then

python manage.py runserver

in anaconda prompt

I’m kinda stumped here. I can’t think of any reasonable explanation for the behavior you’re describing, unless you’ve got some mixup with your directories and you’re editing a different directory than what you’re running with runserver.

Let’s try to clarify your directory structure.

What you should have:

mysite
  |--- mysite
       |--- settings.py
       |--- urls.py
       |--- (other files)
  |--- polls
       |--- apps.py
       |--- views.py
       |--- models.py
       |--- urls.py

There should not be a polls directory inside the “inner” mysite directory

mysite
  db.sqlite3
  manage.py
  urls.py
  |--- mysite
       |--- settings.py
       |--- urls.py
       |---__init__.py
       |---asgi.py
       |---wsgi.py
  |--- polls
       |--- apps.py
       |--- views.py
       |--- models.py
       |--- urls.py
       |--- test.py
       |--- admin.py
       |---__init__.py
       |---migration
          |--- ...

Should I move someting ?

You don’t need to move anything, but you might want to remove that first urls.py file (the one directly below manage.py) - it’s presence may be confusing. Your root urls are in the inner mysite directory as specified in your settings.py file.

If you’ve made your changes to that outer urls.py file, then you have edited the wrong file.