pythondjangoview

When I want to use views.py i give 'QuerySet' object has no attribute error


I want to open course detail with next and previous course but my try doesn't work and I get a 404 error and when I delete try and except I get 'QuerySet' object has no attribute error and says error is from counted_views

enter image description here

views.py:

def course_detail(request,id):
    try:
        course = Courses.objects.get(id=id)
        id_list = []
        course = Courses.objects.filter(status = True)
        for cr in course :
            id_list.append(cr.id)
        
        if id_list[0] == id:
            next_course = Courses.objects.get(id=id_list[1])
            previous_course =None

        elif id_list[-1] == id:
            next_course = None
            previous_course = Courses.objects.get(id=id_list[-2])

        else:
            next_course = Courses.objects.get(id=id_list[id_list.index(id)+1])
            previous_course = Courses.objects.get(id=id_list[id_list.index(id)-1])



        course.counted_views += 1
        course.save()
        context = {
            'course': course,
            'next_course': next_course,
            'previous_course': previous_course,
        }
        return render(request,"courses/course-details.html",context=context)
    except:
        return render(request,'courses/404.html')

urls.py:

from django.urls import path
from .views import *

app_name = 'course'

urlpatterns = [
    path('',Maincourse,name='courses'),
    path('category/<str:cat>',Maincourse,name='course_cat'),
    path('teacher/<str:teacher>',Maincourse,name='course_teacher'),
    path('search/',Maincourse,name='course_search'),
    path('course_detail/<int:id>',course_detail,name='course_detail'),
]

models.py:

class Courses(models.Model):
    title = models.CharField(max_length=40)
    content = models.TextField(max_length=1000)
    teacher = models.ForeignKey(Trainer,on_delete=models.CASCADE)
    price = models.IntegerField(default=0)
    image = models.ImageField(upload_to='courses',default='default.jpg')
    category = models.ManyToManyField(Category)
    counted_views = models.IntegerField(default=0)
    counted_likes = models.IntegerField(default=0)
    status = models.BooleanField(default=False)
    created_date = models.DateTimeField(auto_now_add = True)
    updated_date = models.DateTimeField(auto_now = True)
    available_seats = models.IntegerField(default = 0)
    schedule = models.DateTimeField(default=datetime.datetime.now())

enter image description here


Solution

  • You have declared a variable course initially that fetches an object but then reassigned it to a queryset which now is no longer an object but a list of objects. You can just edit this line from:

    course = Courses.objects.filter(status = True)
    

    To:

    courses = Courses.objects.filter(status = True)
    id_list = [cr.id for cr in courses]
    

    As a side note, by convention, models are named in singular so Course instead of Courses.