Django 啟動項目服務的時候可能會報以下錯誤:以下Apps.userPage是應用名,換成你自己的!!!
單項目不太可能會遇到這個問題,一般多應用放在一個Apps下時可能會出現.doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
File "D:\Fineex WorkSpace\DatasPlat\Apps\userPage\urls.py", line 18, in <module> from . import views File "D:\Fineex WorkSpace\DatasPlat\Apps\userPage\views.py", line 6, in <module> from . import models File "D:\Fineex WorkSpace\DatasPlat\Apps\userPage\models.py", line 6, in <module> class Department(models.Model): File "D:\Fineex WorkSpace\DatasPlat\venv\lib\site-packages\django\db\models\base.py", line 113, in __new__ raise RuntimeError( RuntimeError: Model class Apps.userPage.models.Department doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
如果你確信在models.py中正確添加了對應的Model:
class Department(models.Model): code = models.CharField(max_length=64, blank=False, null=False, primary_key=True, unique=True) name = models.CharField(max_length=128, blank=False, null=False) pcode = models.CharField(max_length=64) desc = models.CharField(max_length=255, blank=True, null=True) lever= models.IntegerField(blank=False, null=False) class Meta: db_table = 'department' verbose_name_plural = db_table
如果你確信在settings.py中已正確注冊App
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'Apps.userPage', .......
且在Apps/userPage/view.py中按照正確路徑(一般為相對路徑)
from . import models
如果你覺得不應該報錯,甚至你Apps下面的其它App沒有這個錯誤,或者刪除上面的引入都可以正常啟動項目的話,錯誤大概率出險在Apps/userPage/apps.py的AppConfig類中,
class UserpageConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'userPage' # 出錯點在這里
改成
class UserpageConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'Apps.userPage'
或者直接在apps.py中刪掉這個Config,再次啟動這個項目,已成功!
只是Django項目啟動報錯的一種情況,不一定適用於其它類型,先判斷好。
