Django —— 报错小记
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视图中存在错误.
提示错误点urls.py中存在非法字符
/project_name/urls.py in <module>, line 28
原因:urls.py中app名字未导入
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文件
可能的原因:settings.py中,"from .middleware import LoginAndPermissionMiddleware",去掉即可。
导入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对象的属性。
优质内容筛选与推荐>>