【发布时间】:2014-05-27 16:18:19
【问题描述】:
作为 Python 初学者,我必须理解这段代码:
from settings import PROJECT_ROOT
--> 我正在尝试在 Python Shell 中输入此内容,但即使我有这样的模块,Python 也会给我一个 Traceback
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
--> 我想使用 Python 内置的数据库 sqlite,但我真的不明白我必须做什么
请原谅我的问题很简单,但我对这些天在 Python 中的任务感到不知所措。
为了完整起见,这是模块中的所有代码,称为settings.py:
from settings import PROJECT_ROOT
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Make this unique, and don't share it with anybody.
SECRET_KEY = ''
# Python dotted path to the WSGI application used by Django's runserver; added in v1.4
WSGI_APPLICATION = 'wsgi.application'
############### PYSEC specific variables
# assumes this directory exists
DATA_DIR = "%s/pysec/data/" % PROJECT_ROOT
更新
我不想强调你已经过度紧张的耐心,但为什么它一直告诉我SECRET_KEY 值为空?我把
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'sdfgtardyure34654356435'
它给了我文字
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
这是cmd的图片
【问题讨论】:
-
您发布的文件是名为
settings.py的文件吗?如果是这样,您不会导入到 same 文件中,而是导入到不同的文件中。 -
是的,文件名为
settings.py -
我尝试在 GUI 中使用此命令,但它不起作用
-
您有两个问题: 1. 您没有使用
import在同一个文件中查找内容。您可以使用它在 another 文件中查找某些内容。 2. 另外,在您的settings.py中有注释,无论如何都会创建一个名为PROJECT_ROOT的值。 -
可能在另一个模块中吗?创建
PROJECT_ROOT的那个?
标签: python django settings python-import