django ImportError 解决方法
2012-03-15 21:54
405 查看
在终端使用模板
提示错误
不接 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
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
相关文章推荐
- python-social-auth with Django: ImportError: No module named 'social_django' 解决方法
- ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 错误解决方法
- ImportError: No module named bz2问题解决方法
- zipimport.ZipimportError的解决方法
- Django 上传文件出现 OSError Permission denied的解决方法
- DjangoUeditor ImportError: No module named ‘widgets’的解决办法
- Django Template Error 及解决方法
- django view.py error 'QuerySet' object has no attribute 'save' 解决方法
- windowXP下解决python2.7+Django-1.3.1中:importError:no module named django.core
- ImportError: No module named setuptools 解决方法
- 安装python第三方库:xlwt及出错ImportError: No module named setuptools的解决方法
- Django在关闭Debug后页面打不开,显示500server error 解决方法
- window下Django第三方APP调用PIL生成验证码出现ImportError: The _imagingft C module is not installed解决
- 使用pip遇到错误ImportError: No module named packaging.version的解决方法
- 从C调用Python脚本unableto load the file system codec ImportError错误解决方法
- 安装PyViz之后报错:ImportError: no module named ns.applications解决方法
- ImportError: No module named cv2的完美解决方法!!!(不能太赞)
- MySQLdb ImportError: libmysqlclient.so.18解决方法
- Import error: no module named cv2 错误解决方法
- Ubuntu+django+apache下配置网页错误OperationalError: Unable to Open Database File 的解决方法