How to solve "Page not found (404)" error in Django?
Asked Answered
B

7

21

My project is named homefood, and when I runserver I get this error.Anybody have any clue how to fix this error.

Page not found (404)

Request Method: GET

Request URL:    http://127.0.0.1:8000/

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

^foodPosts/

^admin/

The current URL, , 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.

My settings.py file looks like this...

import dj_database_url
"""
Django settings for homefood project.

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = 'staticfiles'


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


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DIRS = (

    os.path.join(BASE_DIR, 'templates'),
)


TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']       # Allow all host headers


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'django.contrib.sites',
    'foodPosts',
    'registration',
    'profiles',
    'homefood',
)

MIDDLEWARE_CLASSES = (
    '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 = 'homefood.urls'

WSGI_APPLICATION = 'homefood.wsgi.application'

#=  dj_database_url.config()
#'default': dj_database_url.config(default='mysql://localhost')}
DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'django_db',
    'USER': 'root',
    'PASSWORD': '',
    'HOST': '',
    'PORT': '',
}
}#= dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/New_York'

USE_I18N = True

USE_L10N = True

USE_TZ = True

#Django-registration additions, for account registration
ACCOUNT_ACTIVATION_DAYS=7
EMAIL_HOST = 'localhost'
EMAIL_PORT = 102
EMAIL_HOST_USERNAME = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = '[email protected]'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)                                           #static asset configuration

and my urls.py in my homefood folder is

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'homefood.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^foodPosts/',include('foodPosts.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I think my problem is either in my urls.py, or my settings.py but I am unsure. Any help would be greatly appreciated. Thanks.

Broderick answered 20/11, 2013 at 17:2 Comment(0)
L
28

You are getting the 404 because you haven't defined a url pattern for http://127.0.0.1:8000/ yet.

You should be able to view the admin site at http://127.0.0.1:8000/admin/ and your food posts at http://127.0.0.1:8000/foodPosts/.

To add a url pattern for the homepage, uncomment the following entry in your urls.py, and replace homefood.views.home with the path to the view you want to use.

url(r'^$', 'homefood.views.home', name='home'),
Livelihood answered 20/11, 2013 at 17:12 Comment(0)
E
3

Basically the answer to this to add a entry in the project urls.py file as blank example:

path('', include('MYAPP.urls')),

and in the app urls.py you add this

url('MYAPP', views.index),

make sure in the settings.py you include your app also make sure in the app urls.py you import your views

Eyeless answered 31/8, 2019 at 22:6 Comment(1)
Thank you for help. I can see the page now but only by adding path('', include('MYAPP.urls')), in project's urls.py. I didn't understand why it need to add second line in app's urls.py and also couldn't understand the reason of adding app in settings.py Sorry if you find this question very basic as I'm a newbie in Django. If you can elaborate then would be really helpfulArabist
V
2

I had this error too and the solution was to change double quotes to single quotes for the name= parameter in the urls.py file of the concerned app!

path('register', views.register, name='register')
Violoncellist answered 8/7, 2020 at 23:16 Comment(1)
Thanks for this - I don't know how long I would have been searching for this... otherwiseFlapdoodle
V
1

Not directly relevant to the OP, but maybe useful for others:

My admin pages all went 404 after I accidentally removed the trailing slash from the path() route (django 3.2):

urlpatterns = [
    path('admin', admin.site.urls),  # trailing slash missing...
    path('', include('myapp.urls'))
]

This was easily fixed by restoring to 'admin/'.

Visitation answered 12/7, 2021 at 8:19 Comment(0)
M
0

It's work for me by just added / in the end of 'users' in urls.py app file look like this

path('users/', get_users, name='users')
Marylnmarylou answered 23/12, 2022 at 11:41 Comment(0)
D
0

I got the same error below:

Page not found (404)

Because I didn't put / after test/<int:id> as shown below:

urlpatterns = [     # ↓ Here
    path('test/<int:id>', views.test, name="test")
]

So, I put / after test/<int:id> as shown below, then the error was solved:

urlpatterns = [      # ↓ Here
    path('test/<int:id>/', views.test, name="test")
]
Dutcher answered 12/5, 2023 at 1:4 Comment(0)
T
0

You only see the landing page when there are NO URLS defined:

enter image description here

As soon as you configure a URL (e.g foodPosts/) , the / route will NOT show the landing page unless you explicitly define a / route in urls.py

Hence you don't see that rocket with this link : http://127.0.0.1:8000/

Tardy answered 7/10, 2023 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.