问题
I'm trying to make an SMTP live server as a separate app in django. Whenever I try to run the smtp file python smtp.py
to listen to incoming messages and store them in models, I get ImproperlyConfigured error.
smtp.py
import os
import smtpd
import asyncore
import datetime
from models import IncomingEmail
from email.parser import Parser
class MessageServer():
def __call__(self, peer, mailfrom, recipients, data):
email = Parser().parsestr(data)
incomingDb = IncomingEmail(
sender=mailfrom, recipient=recipients,
message=data, subject=email['subject'],
recevied=datetime.datetime.now()
)
incomingDb.save()
class customSMTPServer(smtpd.SMTPServer):
process_message = MessageServer()
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
server = customSMTPServer(('localhost', 1025), None)
print "Started listening at port 1025"
try:
asyncore.loop()
except KeyboardInterrupt:
server.close()
The project structure:
hello/
manage.py
hello/
__init__.py
settings.py
urls.py
wsgi.py
smtp/
smtp.py
models.py
__init__.py
api/
admin.py
apps.py
urls.py
views.py
The error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environ
ment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
回答1:
Looks like you're getting that error because DB engin is missing some important settings. What if you edit your code like this?
import sys, os
sys.path.append('/path/to/your/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
from django.conf import settings
print "Started listening at port 1025"
...
Or another way is try to move os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
on the top of your file (but after importing os
, of course)
Another a little bit dirty hack is:
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Next thing you could try:
$ ./manage.py shell
...
>>> execfile('your_script.py')
Also, which line throws and exception?
来源:https://stackoverflow.com/questions/35523173/improperlyconfigured-requested-setting-default-index-tablespace-but-settings-a