PyCharm中執行django程式
阿新 • • 發佈:2019-01-24
使用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 <module>
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 <module>
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,具體如下圖所示: