我是Django的新手,正在制作一个搜索栏,并在views.py中添加了逻辑,它引发了此错误“类型对象'About'没有属性'objects'”我不知道为什么
from django.views.generic import TemplateView, ListView, DetailView
from .models import About
#views
class Home(ListView):
"""docstring for Home"""
template_name = 'home.html'
model = About
recents = About.objects.all()
context_object_name = 'recents'
class BiographyDetail(DetailView):
"""docstring for BiographyDetail"""
model = About
template_name = 'details.html'
class About(TemplateView):
"""docstring for BiographyDetail"""
template_name = 'about.html'
model = About
class Contacts(TemplateView):
"""docstring for BiographyDetail"""
template_name = 'contact.html'
model = About
class Projects(TemplateView):
"""docstring for BiographyDetail"""
template_name = 'projects.html'
model = About
class Search(ListView):
"""docstring for BiographyDetail"""
template_name = 'search.html'
model = About
def get_queryset(self): # new
query = self.request.GET.get('search')
searches = About.objects.filter(
Q(name__icontains=query)
)
return searches
这是我的models.py文件夹
#models.py
from django.db import models
from django.utils import timezone
import datetime
# Create your models here.
TYPE = (
('politician','POLITICIAN'),
('poet','POET'),
('author','AUTHOR'),
('actor','ACTOR'),
)
class About(models.Model):
name = models.CharField(max_length = 200, default ='')
quote = models.CharField(max_length = 200, default ='')
born = models.DateField(verbose_name = 'Born on', null = True)
born_at = models.CharField(max_length = 200, default ='')
died = models.DateField(verbose_name = 'Born on', null = True)
died_at = models.CharField(max_length = 200, default ='')
main = models.TextField(default='')
category = models.CharField(choices = TYPE, default = 'poet', max_length = 10)
image = models.ImageField(blank = True, default='',upload_to = 'static')
created_on = models.DateTimeField(auto_now_add = True)
updated_on = models.DateTimeField(auto_now = True)
def __str__(self):
return self.name
def was_published_recently(self):
return self.created_on >=timezone.now() - datetime.timedelta(days=15)
我做错了什么,请帮助我
答案 0 :(得分:2)
您的class About(models.Model):
和class About(TemplateView):
之间似乎有冲突
您没有将导入内容粘贴到文件中,但是我很确定About
和Home
类中的Search
使用的是About
视图您的About
模型。
我建议您重命名一些内容(例如About
-> AboutView
),以便您没有这些完全相同的类名。那应该使事情变得更明显,以便您可以了解发生了什么。