PyCharm中运行django程序

使用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,具体如下图所示:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值