Django1.10 错误:django.contrib.contenttypes.models.ContentType 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用

Posted

技术标签:

【中文标题】Django1.10 错误:django.contrib.contenttypes.models.ContentType 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用程序中【英文标题】:Django1.10 Error: django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS 【发布时间】:2018-12-21 08:58:55 【问题描述】:

我正在使用 Django 1.10,并尝试按照教程使用 django-dynamic-scraper 包:http://django-dynamic-scraper.readthedocs.io/en/latest/getting_started.html

我在调用“python manage.py makemigrations”时遇到了问题:RuntimeError: Model class django.contrib.contenttypes.models.ContentType 没有声明明确的 app_label 并且不在应用程序中在 INSTALLED_APPS 中。

完整版是:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    self.check()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 385, in check
    include_deployment_checks=include_deployment_checks,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 372, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 310, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/zhangjintao/WorkingPlace/dds/dds/urls.py", line 20, in <module>
    url(r'^admin/', admin.site.urls),
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 267, in urls
    return self.get_urls(), 'admin', self.name
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 225, in get_urls
    from django.contrib.contenttypes import views as contenttype_views
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/contenttypes/views.py", line 5, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/contrib/contenttypes/models.py", line 138, in <module>
    class ContentType(models.Model):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 113, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

但我安装的应用程序包含 django.contrib.contenttypes

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'dynamic_scraper.models',
]

我的models.py:

from django.db import models
from dynamic_scraper.models import Scraper, SchedulerRuntime
from scrapy_djangoitem import DjangoItem


class NewsWebsite(models.Model):
    name = models.CharField(max_length=200)
    url = models.URLField()
    scraper = models.ForeignKey(Scraper, blank=True, null=True, on_delete=models.SET_NULL)
    scraper_runtime = models.ForeignKey(SchedulerRuntime, blank=True, null=True, on_delete=models.SET_NULL)

    def __unicode__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(max_length=200)
    news_website = models.ForeignKey(NewsWebsite)
    description = models.TextField(blank=True)
    url = models.URLField()
    checker_runtime = models.ForeignKey(SchedulerRuntime, blank=True, null=True, on_delete=models.SET_NULL)

    def __unicode__(self):
        return self.title


class ArticleItem(DjangoItem):
    django_model = Article

我的 admin.py :

from django.contrib import admin
from .models import Article, NewsWebsite, ArticleItem
# Register your models here.


@admin.register(Article, ArticleItem, NewsWebsite)
class ArticleAdmin(admin.ModelAdmin):
    pass


class NewWebsiteAdmin(admin.ModelAdmin):
    pass


class ArticleItemAdmin(admin.ModelAdmin):
    pass

urls.py 文件:

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

文件夹结构如下:

我很困惑我的代码出了什么问题...

【问题讨论】:

manage.py showmigrations 的输出是什么? 可以添加/dds/dds/urls.py文件吗? @valignatev 我试过了,还是一样的错误。 @JerinPeterGeorge 已上传。 (我没有做任何事情,所以默认是完整的..) 【参考方案1】:

INSTALLED_APPS 中的dynamic_scraper 部分应该没有.models。例如

INSTALLED_APPS = [
    ...
    'dynamic_scraper',
]

此外,由于您使用的是1.10,因此值得为您的应用程序添加一个AppConfig:docs

【讨论】:

刚刚尝试使用 django 1.10 自己安装动态刮板,它对我有用。您使用的是什么确切版本?也许值得在您的问题中发布pip freeze 输出。另外,如果您尝试过,请将dynamic_scraper.models 修复为dynamic_scraper,这样人们就不会给出相同的答案

以上是关于Django1.10 错误:django.contrib.contenttypes.models.ContentType 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.10 模板语法错误。您是不是忘记注册或加载此标签?

Django1.10 - /i18n/setlang/ CSRF 令牌丢失或不正确

如何在python文件中,引用django1.10的model

安装夹具时 Django 1.10 引发错误:没有这样的列:REFERRED.id

Django1.10官方文档翻译

django建站基本步骤