while runnig http://127.0.0.1:8000/blogapp/blogdata/53/ i get error AttributeError at /blogapp/blogdata/53/ 'QuerySet' object has no attribute 'comments' and it says expected error location is views.py line 42 which is
comments=blogmodel.comments.all()
models.py
class Blogmodel(models.Model):
author=models.ForeignKey(User,on_delete=models.CASCADE,blank=True,null=True)
title=models.CharField(max_length=200)
Newpost=models.TextField(max_length=1000)
summary=models.TextField(max_length=50,blank=True)
created_date=models.DateTimeField(auto_now_add=True)
published_date=models.DateTimeField(auto_now=True)
def __str__(self):
return f'{self.title},{self.Newpost},{self.published_date},{self.created_date}'
class commentmodel(models.Model):
blogmodel=models.ForeignKey(Blogmodel,related_name='comments' ,on_delete=models.CASCADE,blank=True,null=True)
comment=models.CharField(max_length=200)
active=models.BooleanField(default=True)
def __str__(self):
return f'{self.comment},{self.blogmodel.title}'
views.py
def blogretrieve(request):
blogmodel=Blogmodel.objects.all().order_by('-published_date')
context={'blogmodel':blogmodel,}
return render(request,'blogapp/blogretrieve.html',context)
def blog(request,pk):
blogmodel=Blogmodel.objects.filter(id=pk)
#comments = y.comments.filter(active=True)
comments=blogmodel.comments.all() #line 42
context={'blogmodel':blogmodel,'comments':comments}
return render(request,'blogapp/blogpk.html',context)
urls.py app_name='blogapp'
urlpatterns=[
path('',views.home,name='home'),
path('createblog/',views.blogview,name='blogview'),
path('blog/',views.blogretrieve,name='blog'),
path('signup/',views.signupview,name='signup'),
path('login/',views.loginview,name='login'),
path('logout/',views.logoutview,name='logout'),
path('author/<str:pk>/',views.authorview,name='author'),
path('blogdata/<str:pk>/',views.blog,name='blogdata'),