This happens when I try to rn server
This photo describes the project structure Django-Python
只是 ,当我尝试导入“发布”模型时,出现此错误,并且无法访问任何页面。
如果我未在views.py中导入任何模型,那么一切看起来都很好。.
您能帮我解决这个问题吗?
from .models import Post
def home_view(request):
latest_posts = Post.objects.order_by('pub_date')[:3]
return render(request, 'home.html', {'Latest': latest_posts})
from django.db import models
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=120)
brief_life = models.TextField(blank=True, null=True)
Date_of_birth = models.DateField()
def __str__(self):
return self.name
class Type(models.Model):
name = models.CharField(max_length=120, unique=True)
description = models.TextField(blank=True, null=True)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=120, unique=True)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
pub_date = models.DateField()
copy_num = models.IntegerField(default=1)
for_buy = models.BooleanField(default=True)
author = models.ForeignKey(Author, on_delete=True)
p_type = models.ForeignKey(Type, on_delete=True)
def __str__(self):
return self.title
from django.apps import AppConfig
class PostsConfig(AppConfig):
name = 'posts'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'project.wsgi.application'
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from src.posts.views import home_view, about_view, details_view, post_info_view
urlpatterns = [
path('', home_view, name='home'),
path('about/', about_view, name='about'),
path('details/', details_view, name='details'),
path('info/', post_info_view, name='info'),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
答案 0 :(得分:0)
您的ForeignKey
on_delete
设置有误。 True
是无效选项。请参考Model Field reference ForeignKey field设置有效选项。如果要在删除外键时删除,请使用models.CASCADE
此外,您已安装的应用为posts
。在您的urls
中,您import
正在from src.posts.views
中。尝试摆脱src.
,它应该可以解决您的问题。
答案 1 :(得分:0)
我已经通过美化代码及其工作方式中的制表符删除空格来解决此问题