使用PyCharm开发django程序,发现如果不在命令行而在IDE的django项目中直接运行django程序,发现报错,程序如下:
def main():
from people.models import Blog
blog = Blog()
blog.name = 'blog1'
blog.tagline = 'tagline1'
blog.save()
if __name__ == '__main__':
main()
print('Done......')
抛出的异常如下:
C:\python\python.exe C:/Users/Administrator/PycharmProjects/mydjango/mytest.py
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 10, in
main()
File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 3, in main
from people.models import Blog
File "C:\Users\Administrator\PycharmProjects\mydjango\people\models.py", line 4, in
class Person(models.Model):
File "C:\Users\Administrator\PycharmProjects\mydjango\people\models.py", line 5, in Person
name = models.CharField(max_length=30)
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\db\models\fields\__init__.py", line 1043, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\db\models\fields\__init__.py", line 166, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 53, in __getattr__
self._setup(name)
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Process finished with exit code 1
经上网查找,增加代码处理如下:
import os
import django
os.environ.setdefault('DJANGO_SETTING_MODULE', 'mydjango.settings')
django.setup()
'''
version = django.get_version()
print(version)
version = (int(version[0]), int(version[2:]))
print(version)
if version >= (1,7):
django.setup()
'''
def main():
from people.models import Blog
blog = Blog()
blog.name = 'blog1'
blog.tagline = 'tagline1'
blog.save()
if __name__ == '__main__':
main()
print('Done......')
运行后发现还是异常,此时是LOGGING_CONFIG没有设置:
C:\python\python.exe C:/Users/Administrator/PycharmProjects/mydjango/mytest.py
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/mydjango/mytest.py", line 5, in <module>
django.setup()
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\django\conf\__init__.py", line 53, in __getattr__
self._setup(name)
File "C:\python\lib\site-packages\django-1.10-py3.5.egg\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.
Process finished with exit code 1
经过查找,终于发现将配置添加到pycharm中的python配置中,可以正常运行程序,配置项的key为DJANGO_SETTINGS_MODULE,value为django的项目名称加点号加settings,我的项目名称为mydjango,所以value为mydjango.settings,具体如下图所示: