Django —— 报错小记


Django —— 小记报错

ORM错误

    job_name = "job_%s_%s_%s" % (flight['org'], flight['dst'], job_create_time)
TypeError: 'Flight' object is not subscriptable

原因:models中的模型类取字通过 "."获取,修改为flight.org flight.dst

from . import views 报错invild

某个app下urls报错,但urls.py中正确。可能原因:views视图中存在错误.

错误

EOL while scanning string literal (urls.py, line 28

提示错误点urls.py中存在非法字符

name 'app_name' is not defined

/project_name/urls.py in <module>, line 28

原因:urls.py中app名字未导入

TypeError: expected str, bytes or os.PathLike object, not NoneType

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000273909BF950>
Traceback (most recent call last):
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check_migrations()
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 163, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 170, in build_graph
    self.load_disk()
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 95, in load_disk
    directory = os.path.dirname(module.__file__)    <===★
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 221, in dirname
    return split(p)[0]
  File "C:\Users\丁伊仙\AppData\Local\Programs\Python\Python37\lib\ntpath.py", line 183, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

manage.py runserver启动django项目后,出现该条报错。

上述★标记处会导入module.__file__,在该处print(module.__file__),结果为多条文件路径,发现最后一条为"None"(遇到None即报错)

于是发现出错原因在于某app下的migrations文件夹为空,缺少__init__.py

手动创建__init__.py,重新启动项目,启动成功。

会按照app注册顺序导入每个注册的app下的migrations文件夹下的__init__.py文件

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

可能的原因:settings.py中,"from .middleware import LoginAndPermissionMiddleware",去掉即可。

'Settings' object has no attribute 'xxxx'

导入settings:from django.conf import settings

在调用settings文件中配置的某个变量时,抛出该错误。但实际上该属性已配置在settings文件中。

查询settings源码,LazySettings类会加载django项目中配置的DJANGO_SETTINGS_MODULE,实例出一个Settings对象 -> Settings(settings_module)。

class Settings(BaseSettings):
    def __init__(self, settings_module):
        # update this dict from global settings (but only for ALL_CAPS settings)
        for setting in dir(global_settings):
            if setting.isupper():
                setattr(self, setting, getattr(global_settings, setting))
    
        # 仅加载自配置settings文件中全大写属性。
        mod = importlib.import_module(self.SETTINGS_MODULE)
        for setting in dir(mod):
            if setting.isupper():
                setting_value = getattr(mod, setting)

                if (setting in tuple_settings and
                        not isinstance(setting_value, (list, tuple))):
                    raise ImproperlyConfigured("The %s setting must be a list or a tuple. "
                            "Please fix your settings." % setting)
                setattr(self, setting, setting_value)
                self._explicit_settings.add(setting)        

★ 故在配置文件settings中的属性,变量名必须为大写,否则不会被设置为Settings对象的属性。

优质内容筛选与推荐>>
1、03构建之法阅读笔记
2、Winform 数据绑定
3、一致性hash算法详解
4、js动态表格
5、npm 提交到私有仓库


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn