简体   繁体   中英

type object 'Album' has no attribute 'object'

I'm new to Django and web coding.

I'm following Bucky tuts: Django Tutorial for Beginners - 29 - Generic Views

& I'm trying to get my music ( index ) page , but it gives me that error in the browser :

AttributeError at /music/ type object 'Album' has no attribute 'object'

& here's my views.py :

from django.http import HttpResponse, Http404
from django.shortcuts import render , get_object_or_404
from .models import Album,song
from django.views import generic

"""
def index(request):
    all_albums = Album.objects.all()
    context = {'all_albums': all_albums}
    return render(request, 'music/index.html', context)
"""

class IndexView (generic.ListView):
    template_name = 'music/index.html'
    context_object_name = 'all_albums'

    def get_queryset(self):
        return Album.object.all()

'''
class DetailView (generic.DetailView):
    model = Album
    template_name = "music/details.html"
'''


def details(request, album_id):
    try:
        album = Album.objects.get(pk=album_id)
    except Album.DoesNotExist:
        raise Http404("Album Does Not Exists !")
    return render(request, 'music/details.html', {'album': album})

def favourite (request , album_id):
    album = get_object_or_404 (Album , pk=album_id)
    try:
        selected_song = album.song_set.get(pk=request.POST['song'])
    except(KeyError, song.DoesNotExist):
        return render(request, 'music/details.html', {
            'album':album,
            'error_message': "you entered wrong"
        })
    else:
        selected_song.is_favorite = False
        selected_song.save()


        return render(request,'music/details.html' , {'album':album})

models.py

from django.db import models

# Create your models here.

class Album (models.Model):
    artist = models.CharField(max_length = 100)
    album_title = models.CharField(max_length = 100)
    genre = models.CharField(max_length = 50)
    album_logo = models.CharField(max_length = 1000)

    def __str__(self):
        return self.album_title + " - " + self.artist


class song (models.Model):
    album = models.ForeignKey(Album , on_delete=models.CASCADE)
    file_type = models.CharField(max_length = 10)
    song_title = models.CharField(max_length = 100)
    is_favourite = models.BooleanField(default=False)
    def __str__(self):
        return self.song_title

index.html

{% extends 'music/base.html' %}
{% block title %}Main : MuSiC {% endblock %}

{% block body %}
<ul>
    {% for album in all_albums %}
    <li><a href="{% url 'music:details' album.id %}">{{ album.album_title }}</a></li>
    {% endfor %}
</ul>
{% endblock %}

#/music/{{ album.id }}

project structure

{ (website) project dir }
|-music
..|-migrations
..|-static
..|-templates
....|-music
......|-base.html
......|-details.html
......|-index.html
|-__init__.py
|-admin.py
|-apps.py
|-models.py
|-tests.py
|-urls.py
|-views.py
|-website
..|-__init__.py
..|-settings.py
..|-urls.py
..|-wsgi.py
|-db.sqlite3
|-manage.py

and I don't know where is the problem :(

btw, lot's of coding terms I still didn't learned , that's why I may ask alot ever I searched for a solution but didn't understand the answer from other question's answers .

Album.object does not exist; you should've written Album.objects .

class IndexView (generic.ListView):
    template_name = 'music/index.html'
    context_object_name = 'all_albums'

    def get_queryset(self):
        # return Album.object.all()  <-- Remove this
        return Album.objects.all()

As a side note, reserved words cannot be python attributes. This is by design, because disallowing these words makes parsing substantially easier.

Why can't attribute names be Python keywords?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM