Django例外:django.core.exceptions.ImproperlyConfigured: [英] Django exception : django.core.exceptions.ImproperlyConfigured:

查看:233
本文介绍了Django例外:django.core.exceptions.ImproperlyConfigured:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在django shell中运行相同的代码时,对我来说效果很好.但是当我启动Python解释器(Python 2)进行检查时,尝试导入时出现错误-从django.contrib.auth.models import User

When i run the same code in django shell it works fine for me. but when I fire up the Python interpreter (Python 2) to check some things, I get the an error when I try importing - from 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.

对此非常感谢.

推荐答案

为了运行良好,需要设置Django,当您通过manage.py shell运行它时,manage.py会为您照顾或设置它,但通过python脚本进行此操作需要手动设置.

Django needs to be setup, in order to run fine, when you run it through manage.py shell, manage.py takes care or setting it up for you, but doing it via a python script, needs manual setup.

您似乎还使用了定义的模型,为了能够将其导入到python脚本中,您需要将项目根目录的路径添加到当前python路径.

You also seem to have used your defined models, to be able to import them into your python script, you need to add the path to the root folder of your project to the current python path.

最后,您需要告诉django 设置文件在哪里( 设置django之前),在manage.py文件中,您应该有类似以下内容:

Finally, you need to tell django where your settings file is (before setting up your django), in your manage.py file, you should have something like this :

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

将其设为常量,将其命名为* DEFAULT_SETTINGS_MODULE *,因此您现在拥有:

Go make it a constant, name it * DEFAULT_SETTINGS_MODULE *, so you now have:

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

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

Now you need to import the constant into your script and tell django (By setting an env var) where it should look for the settings file.

总而言之:

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

这样,您的设置就可以了.

This way you're setup up just fine.

这篇关于Django例外:django.core.exceptions.ImproperlyConfigured:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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