作为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.
}
}
- >我想使用sqlite内置于Python的db,但我真的无法理解我必须做什么
请原谅我这个问题的基本性,但是我觉得这些天我在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
答案 0 :(得分:1)
尝试使用python manage.py shell
打开python shell。
通常settings.py
文件驻留在项目根目录中,因此为了导入PROJECT_ROOT
变量,您可以使用from project_name.settings import PROJECT_ROOT
<强> [编辑] 强>
要使用sqlite引擎,请将DATABASES
字典更改为:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_ROOT, '/project_database/db_name.sqlite3'),
}
}
<强> 2 [编辑] 强>
没有压力。像提示一样,请参阅此Adding Python Path on Windows 7问题将python文件添加到win path变量中,这有助于您避免将项目放在c:PythonXX中,而是使用其他目录。
我看一下链接的github项目,它似乎在README文件中解释你必须添加一个SECRET_KEY和一个DATA_DIR变量。
这是我为完成项目工作而采取的解决方法:
$ git clone https://github.com/lukerosiak/pysec.git
$ cd pysec
$ ls # the dir command when on Windows
README.md
TODO.md
local_settings-example.py
manage.py*
pysec/
requirements.txt
settings.py*
$ cp local_settings-example.py local_settings.py
编辑local_settings.py
文件并修改SECRET_KEY和DATA_DIR变量:
SECRET_KEY = '@r65u-33v3vu-e^h-%u4kg=g9y5z'
DATA_DIR = '/home/slacker/pysec/project_database' # or something like: C:\\users\tosh\pysec\
project_database 运行:
$ python manage.py syncdb
我希望它可以提供帮助!