简体   繁体   中英

Django 404 error-page not found

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 = 'testing@example.com'

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.

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

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

我也有这个错误,解决方案是将相关应用程序的urls.py文件中name=参数的双引号更改为单引号!

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

You may also want to check your method in the html, just in case. For example : <form method="POST" action="">

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/' .

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM