配置不正确:请求的设置为DEFAULT_INDEX_TABLESPACE,但未配置设置 [英] ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured

查看:74
本文介绍了配置不正确:请求的设置为DEFAULT_INDEX_TABLESPACE,但未配置设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使SMTP实时服务器作为django中的单独应用程序.每当我尝试运行smtp文件 python smtp.py 来侦听传入的消息并将其存储在模型中时,都会收到ImproperlyConfigured错误.

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()

项目结构:

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

错误:

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.

推荐答案

由于DB引擎缺少一些重要设置,因此您似乎收到了该错误.如果您这样编辑代码怎么办?

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"
...

或者另一种方法是尝试将 os.environ.setdefault("DJANGO_SETTINGS_MODULE","hello.settings")移动到文件顶部(但在导入 os ,当然)另一个有点肮脏的技巧是:

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)

接下来您可以尝试:

$ ./manage.py shell
...
>>> execfile('your_script.py')

还有,哪一行引发异常?

Also, which line throws and exception?

这篇关于配置不正确:请求的设置为DEFAULT_INDEX_TABLESPACE,但未配置设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆