我正在关注用于部署我的应用程序的两个django和mozilla教程。 我设置了虚拟环境和基本设置,本地设置等,当我运行命令时:
./manage.py runserver 0:8000 --settings=Books.settings.local
浏览器显示错误,找不到模板主页... 我认为由于env可能存在一些问题,我不确定。
base.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2z9y'
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
ALLOWED_HOSTS = ['127.0.0.1', 'localhost','https://hackingonlineeducation.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#third party apps
'star_ratings',
'crispy_forms',
#my_apps
'newsletter',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'Books.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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 = 'Books.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR,"static","bootstrap")
# '/var/www/static/',
]
STATIC_ROOT=os.path.join(BASE_DIR,'static','static_root')
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media','profile_images')
#crispy_forms
CRISPY_TEMPLATE_PACK = 'bootstrap3'
LOGIN_REDIRECT_URL='profile'
LOGOUT_REDIRECT_URL='home'
local.py
from Books.settings.base import *
DEBUG = True
INSTALLED_APPS += (
'debug_toolbar', # and other apps for local development
)
MIDDLEWARE +=[
'debug_toolbar.middleware.DebugToolbarMiddleware'
]
浏览器中的错误:
Exception Location: /home/user/usr/lib/python3.6/site-packages/django/template/loader.py in get_template, line 25
Python Executable: /home/user/usr/bin/python
Python Version: 3.6.1
Python Path:
['/home/user/workspace/Books',
'/home/user/usr/lib/python36.zip',
'/home/user/usr/lib/python3.6',
'/home/user/usr/lib/python3.6/lib-dynload',
'/home/user/usr/lib/python3.6/site-packages',
'/home/user/usr/lib/python3.6/site-packages/Sphinx-1.5.6-py3.6.egg',
'/home/user/workspace/speakerfight/src/django-queryset-csv',
'/home/user/workspace/speakerfight/src/django-activelink']
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /home/user/workspace/Books/Books/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/django/contrib/auth/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/star_ratings/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/crispy_forms/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/debug_toolbar/templates/home.html (Source does not exist)
我还更新了manage.py,如下所示:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Books.settings.local")
答案 0 :(得分:0)
这一行:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file __)))
将BASE_DIR指定为base.py所在的当前目录的父目录。这意味着BASE_DIR将引用/home/user/workspace/Books/Books/
你应该试试这个:
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_ROOT)
然后在TEMPLATES:
:
TEMPLATES = [
{
...
'DIRS': [os.path.join(PROJECT_ROOT,'templates')],
...
]