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

  • 相关阅读:
    BZOJ1085 SCOI2005 骑士精神【IDA* 启发式迭代加深】
    BZOJ1690 Usaco2007 Dec 奶牛的旅行 【01分数规划】
    SPOJ104 Highways 【矩阵树定理】
    BZOJ1597土地购买 【斜率优化DP】
    【模板】NTT
    【模板】FFT
    BZOJ3196 Tyvj1730 二逼平衡树 【树套树】 【线段树套treap】
    POJ3696 The Windy's 【网络流】
    POJ2728 Desert King 【最优比率生成树】
    BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*
  • 原文地址:https://www.cnblogs.com/cacique/p/2398782.html
Copyright © 2011-2022 走看看