【问题标题】:Django 3 - doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPSDjango 3 - 没有声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中
【发布时间】:2020-06-17 00:36:29
【问题描述】:

我正在学习 Django 3,但遇到了问题。我的应用程序称为 calc1。代码如下:

MODELS.PY

from django.db import models

# Create your models here.

class Dreamreal(models.Model):
    website = models.CharField(max_length = 50)
    mail = models.CharField(max_length = 50)
    name = models.CharField(max_length = 50)
    phonenumber = models.IntegerField()

class Meta:
    db_table = "dreamreal"

VIEWS.PY

from django.shortcuts import render
from django.http import HttpResponse
import datetime
import time
from .models import Dreamreal
from django.http import HttpResponse



# Create your views here.
def home(request):
    today = datetime.datetime.now().date()

    return render(request, 'home.html',{'today' :today}) 


def crudops(request):        
    dreamreal = Dreamreal(
      website = "www.vlcbt.org.uk", mail = "info@vlcbt.org.uk",
      name = "John", phonenumber = "08767655665"
    )

    dreamreal.save()

    # read all entries and print
    objects = Dreamreal.objects.all()
    res ="printing all documents <br>"
    for elt in objects: 
        res += elt.name +"<br>"

    return HttpResponse(res)

当我尝试迁移时,我收到以下错误消息:

文件“C:\Users\john\Envs\lms\Scripts\projects\jkjlms\calc1\urls.py”,第 3 行,在 从 。导入视图 文件“C:\Users\john\Envs\lms\Scripts\projects\jkjlms\calc1\views.py”,第 5 行,在 从 .models 导入 Dreamreal 文件“C:\Users\john\Envs\lms\Scripts\projects\jkjlms\calc1\models.py”,第 5 行,在 类 Dreamreal(models.Model): new 中的文件“C:\python\lib\site-packages\django\db\models\base.py”,第 115 行 “INSTALLED_APPS。” %(模块,名称) RuntimeError: 模型类 calc1.models.Dreamreal 没有明确声明 app_label 并且不在 INSTALLED_APPS 中的应用程序中。

提前感谢您的帮助

【问题讨论】:

    标签: python django


    【解决方案1】:

    您应该在 INSTALLED_APPS 的 settings.py 文件中添加您的应用:

    INSTALLED_APPS = [
        # ...,
        calc1,
    ]
    

    另外,在迁移之前你应该做python manage.py makemigrations calc1

    【讨论】:

      【解决方案2】:

      只需将 Django 的站点框架添加到您的应用中,并在您的设置中将 SITE_ID 设置为 1。

      INSTALLED_APPS = [
          ...
          'django.contrib.sites',
      ]
      
      SITE_ID = 1
      
      

      【讨论】:

        猜你喜欢
        • 2018-10-19
        • 2018-01-03
        • 2023-02-18
        • 1970-01-01
        • 2019-12-23
        • 2020-08-13
        • 2018-12-21
        • 2021-10-23
        • 1970-01-01
        相关资源
        最近更新 更多