I am learning Django
and got the following error.
AttributeError at 'list' object has no attribute 'objects'
This is my code:
Models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class post(models.Model):`
title = models.CharField(max_length = 100)
content = models.TextField()
date_posted = models.DateTimeField(default = timezone.now)
author = models.ForeignKey(User,on_delete = models.CASCADE)
def __str__(self):
return self.title
Views.py :
from django.shortcuts import render
from .models import post
#lets add some dummy data
post = [
{
'author':'raj kumar',
'title' :'blog title 1',
'date_posted':'27 August 2020 ',
"content" :"fist post content"
},
{
'author': "kathir",
'title': 'blog title 2',
'date_posted':"28 August 2020 ",
"content" :"second post content"
}
]
def home(request):
context = {
"posts": post.objects.all()
}
return render(request,'blog/home.html',context)
def blog(request):
return render(request,'blog/blog.html')
Django
ORM
. I inserted some data using the post
class.>>post.objects.all()
<QuerySet [<post:blog1>,<post:blog2>,<post:blog3>]
You're treating post like an object. It's not. It's a list of dictionaries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.