我之前问过这个,但我仍然卡住了。该脚本最终将作为 cron 作业运行。
上一个问题:将 CSV 导入 Django 和无法识别的设置
我跳过了导入 csvs 的实际代码,因为这不是问题。
import urllib2
import csv
import requests
from django.core.management import setup_environ
from django.db import models
from gmbl import settings
settings.configure(
DEBUG = True,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/Users/c/Dropbox/Django/mysite/mysite/db.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
)
#
from sys import path
sys.path.append("/Users/chris/Dropbox/Django/mysite/gmbl")
from django.conf import settings
这给了我回溯:django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
logout
所以我尝试切换它,并将 settings.configure... 等放在该from django.db import models
行之前,但它只是说"settings not defined"
我试过添加
from django.core.management import setup_environ
from django.db import models
from yoursite import settings
setup_environ(settings)
答案中建议的代码,但在 from django.db import models 部分仍然出错。除了对其他人来说非常明显的东西之外,我还缺少什么?