Django异常:django.core.exceptions.ImproperlyConfigured:

时间:2016-08-25 05:30:04

标签: python django

当我在django shell中运行相同的代码时,它对我来说很好。但是当我启动Python解释器(Python 2)来检查某些东西时,我在尝试导入时遇到错误 - 来自django.contrib.auth.models import User

import os
import django
django.setup()
import smtplib
import sys
sys.path.insert(0, "/edx/app/edxapp/edx-platform/lms/djangoapps/courseware")
import dateutil.relativedelta

from django.conf import settings
from django.contrib.auth.models import User
from opaque_keys.edx.keys import CourseKey
from courseware.models import StudentModule

from datetime import datetime



def PCSurvey_email():
    for student in StudentModule.objects.filter(module_type="PCSurvey"):
        two_months_date =  datetime.now().date() - dateutil.relativedelta.relativedelta(months=2)
        created = student.created.date()
        if created == two_months_date :
            user_id = student.student_id
            user_email = User.objects.get(id = user_id).email
            FROM = "user@example.com"
            TO = [user_email] # must be a list
            SUBJECT = "Hello!"
            TEXT = "This message was sent with Python's smtplib."
                        # Prepare actual message

            message = """\
            From: %s
            To: %s
            Subject: %s
            %s
            """ % (FROM, ", ".join(TO), SUBJECT, TEXT)

                # Send the mail

            server = smtplib.SMTP('outlook.net')
            server.sendmail(FROM, TO, message)
            server.quit()

     #deleting the module
            smod = StudentModule.objects.get(module_type="PCSurvey", course_id = student.course_id, student_id= student.student_id)
            smod.delete()

我得到的错误是

    Traceback (most recent call last):
  File "/edx/app/edxapp/edx-platform/lms/djangoapps/courseware/PCSurvey_email.py", line 4, in <module>
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

对此非常感谢。

3 个答案:

答案 0 :(得分:7)

Django需要设置,为了运行正常,当你通过manage.py shell运行它时,manage.py会照顾或为你设置它,但是通过python脚本来完成它需要手动设置。 / p>

您似乎也使用了已定义的模型,以便能够将它们导入到python脚本中,您需要将项目的根文件夹的路径添加到当前的python路径。

最后,你需要告诉django 设置文件的位置(设置你的django之前),在你的manage.py文件中,你应该有这样的东西:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

将它设为常数,将其命名为* DEFAULT_SETTINGS_MODULE *,所以你现在拥有:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", DEFAULT_SETTINGS_MODULE)

现在您需要将常量导入到脚本中并告诉django(通过设置env var)它应该在哪里查找设置文件。

总而言之:

import sys, os
sys.path.insert(0, "/path/to/parent/of/courseware") # /home/projects/my-djproj

from manage import DEFAULT_SETTINGS_MODULE
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DEFAULT_SETTINGS_MODULE)

import django
django.setup() 

这样你就可以很好地设置了。

答案 1 :(得分:1)

当您打开shell时,django会自动设置自己。

使用Python解释器,您必须手动执行此操作:

import django
django.setup()

答案 2 :(得分:0)

对于遇到此问题的新用户。 如果您不想按照@SpiXel的精彩描述手动设置Django设置文件 您可以从项目目录运行python manage.py shell,该目录将基本上自动链接django需要的设置文件。