简体   繁体   English

类型对象“相册”没有属性“对象”

[英]type object 'Album' has no attribute 'object'

I'm new to Django and web coding. 我是Django和网络编码的新手。

I'm following Bucky tuts: Django Tutorial for Beginners - 29 - Generic Views 我关注的是Bucky tut:面向初学者的Django教程-29-通用视图

& 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' / music /类型对象“相册”中的AttributeError没有属性“对象”

& here's my views.py : 这是我的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 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 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; Album.object不存在; you should've written Album.objects . 您应该已经编写了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. 另外,保留字不能是python属性。 This is by design, because disallowing these words makes parsing substantially easier. 这是设计使然,因为禁止使用这些字词会使解析变得非常容易。

Why can't attribute names be Python keywords? 为什么属性名称不能是Python关键字?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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