我正在尝试在Linode服务器上托管网站。我实际上正在关注YouTube上的Corey Schafer教程,但现在我陷入了困境。因此,在这一点上,我正在服务器中运行带有SSH的命令,但是当我运行任何manage.py命令时,尤其是python3 manage.py runserver 0.0.0.0:8000时,标题中出现了错误。这是完整的错误:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
super().execute(*args, **options)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/taufiq/trident_site/server_env/lib/python3.7/site-packages/django/conf/__init__.py", line 161, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
settings.py:
"""
Django settings for trident_site project.
Generated by 'django-admin startproject' using Django 3.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
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__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = 'f!q(_!a4knufqo%6l$)pi=@a3lgv7tkm$l&9(pb@i44cq+&4vr'
SECRET_KEY = "d2447c3914f7daa852deed834467db03e785ef02f2128d42"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["tridentdigital.herokuapp.com", "127.0.0.1", "tridentdigital.sg"]
STATICFILES_DIRS = (
os.path.join('static'),
)
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#my apps
'trident_pages',
]
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 = 'trident_site.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 = 'trident_site.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trident_site.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
[我尝试在服务器控制台上使用nano settings.py进行打印(SECRET_KEY),但显示为None。
我一直在搜索几个小时来尝试解决相同问题的解决方案,但无法正常工作。如果我需要显示其他内容以进行澄清,我很乐意这样做。
编辑:这是我的目录树(3个级别)
── Procfile
├── README.md
├── db.sqlite3
├── listing.txt
├── manage.py
├── pip3 -> usr/local/bin/pip3
├── requirements.txt
├── runtime.txt
├── static
│ ├── mdbootstrap
│ │ ├── css
│ │ ├── img
│ │ ├── index.html
│ │ ├── js
│ │ ├── scss
│ │ └── src
│ ├── scroll-reveal
│ │ ├── README.html
│ │ ├── README.md
│ │ ├── css
│ │ ├── index.html
│ │ └── js
│ └── trident_pages
│ ├── Kollektif.ttf
│ ├── images
│ ├── modal.css
│ └── navbar.css
├── staticfiles
├── [email protected]
├── trident_env
│ ├── Lib
│ │ └── site-packages
│ ├── Scripts
│ │ ├── Activate.ps1
│ │ ├── __pycache__
│ │ ├── activate
│ │ ├── activate.bat
│ │ ├── deactivate.bat
│ │ ├── django-admin.exe
│ │ ├── django-admin.py
│ │ ├── easy_install-3.8.exe
│ │ ├── easy_install.exe
│ │ ├── pip.exe
│ │ ├── pip3.8.exe
│ │ ├── pip3.exe
│ │ ├── python.exe
│ │ ├── pythonw.exe
│ │ └── sqlformat.exe
│ └── pyvenv.cfg
├── trident_pages
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── __init__.cpython-38.pyc
│ │ ├── admin.cpython-37.pyc
│ │ ├── admin.cpython-38.pyc
│ │ ├── forms.cpython-37.pyc
│ │ ├── forms.cpython-38.pyc
│ │ ├── models.cpython-37.pyc
│ │ ├── models.cpython-38.pyc
│ │ ├── urls.cpython-37.pyc
│ │ ├── urls.cpython-38.pyc
│ │ ├── views.cpython-37.pyc
│ │ └── views.cpython-38.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0002_auto_20200422_2020.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ ├── models.py
│ ├── templates
│ │ └── trident_pages
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── trident_site
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-37.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-37.pyc
│ │ ├── urls.cpython-38.pyc
│ │ ├── wsgi.cpython-37.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── venv
├── bin
│ ├── __pycache__
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate.ps1
│ ├── activate.xsh
│ ├── activate_this.py
│ ├── django-admin
│ ├── django-admin.py
│ ├── easy_install
│ ├── easy_install-3.7
│ ├── easy_install3
│ ├── epylint
│ ├── gunicorn
│ ├── isort
│ ├── pip
│ ├── pip-3.7
│ ├── pip3
│ ├── pip3.7
│ ├── pylint
│ ├── pyreverse
│ ├── python -> /usr/local/opt/python/bin/python3.7
│ ├── python3 -> python
│ ├── python3.7 -> python
│ ├── sqlformat
│ ├── symilar
│ ├── wheel
│ ├── wheel-3.7
│ └── wheel3
├── lib
│ └── python3.7
└── pyvenv.cfg
我解决了这个问题。原来,由于某种原因,当我将目录scp放入服务器时,它创建了2个不同的settings.py,其中一个位于项目名称所在的目录中。我在本地计算机上编辑的那个更改已保存到与manage.py相同的目录中的settings.py中。因此,要解决该问题,我将SECRET_KEY添加到了项目名称所在的目录中。