如何解决Django3x RuntimeError:
我正在学习Django 3x,当我尝试在config / urls.html中添加路径时 “路径(起司/', include('everycheese.cheeses.urls',namespace ='cheeses'), ),”它会引发运行时错误,“ RuntimeError:模型类everycheese.cheeses.models.Cheese未声明显式的app_label,并且不在INSTALLED_APPS中的应用程序中。”
我搜索了解决方案,但发现现有答案无法解决该问题。
有人可以帮我吗?
谢谢。
./config/urls.html
...
urlpatterns = [
path(
"",TemplateView.as_view(template_name="pages/home.html"),name="home",),path(
"about/",TemplateView.as_view(template_name="pages/about.html"),name="about",# Django Admin,use {% url 'admin:index' %}
path(settings.ADMIN_URL,admin.site.urls),# User management
path(
"users/",include("everycheese.users.urls",namespace="users"),path("accounts/",include("allauth.urls")),# Your stuff: custom urls includes go here
path(
'cheeses/',include('everycheese.cheeses.urls',namespace='cheeses'),] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
...
./cheeses/models.py
from django.db import models
from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel
class Cheese(TimeStampedModel):
class Firmness(models.TextChoices):
UNSPECIFIED = "unspecified","Unspecified"
SOFT = "soft","Soft"
SEMI_SOFT = "semi-soft","Semi-Soft"
SEMI_HARD = "semi-hard","Semi-Hard"
HARD = "hard","Hard"
name = models.CharField("Name of Cheese",max_length=255)
slug = AutoSlugField("Cheese Address",unique=True,always_update=False,populate_from="name")
description = models.TextField("Description",blank=True)
firmness = models.CharField("Firmness",max_length=20,choices=Firmness.choices,default=Firmness.UNSPECIFIED)
def __str__(self):
return self.name
解决方法
您使用的路径错误,应该用于重定向到应用程序上的url.py页面。
与其说你在做什么,不如说是一条路:
path('',include('NAMEOFAPP.urls')),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。