Python ';功能';对象没有属性';对象';Django,帮帮我
我正在设计Django应用程序,遇到一条错误消息:Python ';功能';对象没有属性';对象';Django,帮帮我,python,django,Python,Django,我正在设计Django应用程序,遇到一条错误消息: AttributeError at / 'function' object has no attribute 'objects' Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.2.10 Exception Type: AttributeError Exception Value: 'function' object has no attribu
AttributeError at /
'function' object has no attribute 'objects'
Request Method:
GET
Request URL:
http://127.0.0.1:8000/
Django Version:
2.2.10
Exception Type:
AttributeError
Exception Value:
'function' object has no attribute 'objects'
这是生成消息的my views.py:
from django.shortcuts import render
from post.models import posts
def index(request):
featured = Post.objects.filter(featured=True)
context = {
'object_list': featured
}
return render(request, 'index.html', context)
def blog(request):
return render(request, 'blog.html', {})
def Post(request):
return render(request, 'post.html', {})`
这是我的模特
from django.db import models
from django.contrib.auth import get_user_model
user = get_user_model()
class author(models.Model):
user = models.OneToOneField(user, on_delete=models.CASCADE)
profile_picture = models.ImageField
class category(models.Model):
title = models.CharField(max_length=20)
def __str__(self):
return self.title
class posts(models.Model):
title = models.CharField(max_length=100)
overview = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
comment_count = models.IntegerField(default=0)
author = models.ForeignKey(author, on_delete=models.CASCADE)
thumbnail = models.ImageField()
categories = models.ManyToManyField(category)
featured = models.BooleanField()
def __str__(self):
return self.title
谢谢您的帮助。您编写了一个名为
Post
的视图函数,因此Post.objects
指的是Post
函数,而不是模型。此外,您还将模型命名为posts
,而不是Post
。我强烈建议将您的模型重命名为Post
,因为Django模型通常是单数的,并且是用PerlCase编写的:
从django.exe导入渲染
从post.models导入post
def索引(请求):
特色=posts.objects.filter(特色=True)
上下文={
“对象列表”:特色
}
返回呈现(请求'index.html',上下文)
def博客(请求):
返回呈现(请求'blog.html',{})
def post(请求):
返回呈现(请求'post.html',{})
当然,您应该相应地重命名变量。您编写了一个名为
Post
的视图函数,因此Post。对象
指的是Post
函数,而不是模型。此外,您还将模型命名为posts
,而不是Post
。我强烈建议将您的模型重命名为Post
,因为Django模型通常是单数的,并且是用PerlCase编写的:
从django.exe导入渲染
从post.models导入post
def索引(请求):
特色=posts.objects.filter(特色=True)
上下文={
“对象列表”:特色
}
返回呈现(请求'index.html',上下文)
def博客(请求):
返回呈现(请求'blog.html',{})
def post(请求):
返回呈现(请求'post.html',{})
当然,您应该相应地重命名变量
from django.shortcuts import render
from post.models import posts
def index(request):
featured = posts.objects.filter(featured=True)
context = {
'object_list': featured
}
return render(request, 'index.html', context)
def blog(request):
return render(request, 'blog.html', {})
def post(request):
return render(request, 'post.html', {})