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

  • 相关阅读:
    [转载]SQL Server行列转换实现
    [转载]依赖注入那些事
    [转载]MVC、MVP以及Model2(下)
    [转载]MVC、MVP以及Model2(上)
    SQL Server中CROSS APPLY和OUTER APPLY应用
    Open XML操作Excel导入数据
    centos7 firewall-cmd 理解多区域配置中的 firewalld 防火墙
    centos7 firewall-cmd 用活firewalld防火墙中的zone
    个税起征点上调,你每月能省多少?最后还有点凉水
    2018年中国500强排行榜
  • 原文地址:https://www.cnblogs.com/cacique/p/2398782.html
Copyright © 2011-2022 走看看