zoukankan      html  css  js  c++  java
  • django ImportError 解决方法

    在终端使用模板

    from django import template
    t = template.Template('My name is {{ name }}.')


    提示错误 

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Library/Python/2.7/site-packages/django/template/base.py", line 123, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
    File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
    File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
    ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

    不接 google 然后找到如下答案

    If you're playng with Django directly from Python, and not from Django's own manage.py shell, then you need to set the DJANGO_SETTINGS_MODULE variable before launching Python.

    For example, if you have a Django project named "myproject", then run the following before launching Python:

    export DJANGO_SETTINGS_MODULE=myproject.settings

    For Apache to pick up that variable, you need to set it in httpd.conf as follows:

    SetEnv DJANGO_SETTINGS_MODULE myproject.settings

     

    意思就是 

    在命令行下使用python命令进入交互模式的,Template(str)在构照模板的时候依赖django.conf.settings.这个变量,但是在当前的环境中没有这个变量,这时,它就会抛出异常了

     

    办法就是 要想使用django的模板你必须导入settings

    >>> from django.conf import settings

    >>> settings.configure()

    你在运行t = Template("my name is {{name}}") 就不会出错了

     

    还有一种方法就是使用python manager.py shell运行

     

    manager.py在项目目录下

    现在就一切正常

    参考链接  http://iyouf.info/fixed-django-settings-error.html

                 http://forum.webfaction.com/viewtopic.php?id=2282

  • 相关阅读:
    并发容器梳理
    CAS总结
    原子类总结and-Git提交出现error: src refspec master does not match any的问题
    简单工厂模式小结
    JVM学习与问题总结——java内存区域与内存溢出异常
    反射机制学习记录
    观察者模式
    IDEA的一些常用设置
    建造者模式
    【[AH2017/HNOI2017]礼物】
  • 原文地址:https://www.cnblogs.com/cacique/p/2398782.html
Copyright © 2011-2022 走看看