PyCharm 在django程序中单独运行py文件

使用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,具体如下图所示: 

这里写图片描述 
这里写图片描述 
这里写图片描述 
这里写图片描述 
这里写图片描述

posted on   星河赵  阅读(4658)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
历史上的今天:
2016-12-19 jquery checkbox的相关操作——全选、反选、获得所有选中的checkbox

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示