在django中使用ListView,获取__init __()只需1个参数(给定2个)

Man*_*a89 1 django django-templates django-views django-class-based-views

我是django的新手,我正在尝试显示我在数据库中的专辑列表.这是专辑模型

class Album(models.Model):
"""Album model"""
  title = models.CharField(max_length=255)
  prefix = models.CharField(max_length=20, blank=True)
  subtitle = models.CharField(blank=True, max_length=255)
  slug = models.SlugField()
  band = models.ForeignKey(Band, blank=True)
  label = models.ForeignKey(Label, blank=True)
  asin = models.CharField(max_length=14, blank=True)
  release_date = models.DateField(blank=True, null=True)
  cover = models.FileField(upload_to='albums', blank=True)
  review = models.TextField(blank=True)
  genre = models.ManyToManyField(Genre, blank=True)
  is_ep = models.BooleanField(default=False)
  is_compilation = models.BooleanField(default=False)

  class Meta:
    db_table = 'music_albums'
    ordering = ('title',)

  def __unicode__(self):
    return '%s' % self.full_title
Run Code Online (Sandbox Code Playgroud)

我的观点是

    class album_list(ListView):
        template_name = "/music/album_list.html"
        context_object_name = 'list_of_albums'
       #paginate_by = '15'

        def get_queryset(self):
           return Album.objects.all()
Run Code Online (Sandbox Code Playgroud)

我可以从管理界面添加专辑,但是在转到/ albums/url显示它们时,我得到init()只需要1个参数(给定2个)错误.

我正在使用的模板

    {% extends "music/base_music.html" %}

    {% block title %}Music Albums{% endblock %}
    {% block body_class %}{{ block.super }} music_albums{% endblock %}


    {% block content_title %}
      <h2>Music Albums</h2>
      {% include "music/_nav.html" %}
    {% endblock %}


    {% block content %}
     <table>
     <tr>
     <th>Band</th>
     <th>Album</th>
     </tr>
     {% for album in list_of_albums %}
      <tr class="{% cycle 'odd' 'even' %}">
      <td class="band"><a href="{{ album.band.get_absolute_url }}">{{ album.band }}</a>  </td>
      <td class="album"><a href="{{ album.get_absolute_url }}">{{ album.full_title }}</a></td>
      </tr>
      {% endfor %}

      </table>
      {% endblock %}
Run Code Online (Sandbox Code Playgroud)

我已经解决了此处已经提到的类似问题的答案,但无法使代码生效.

fas*_*uto 10

通常这是因为你忘记.as_view()了你的urls.py:

代替

(r"", 'SomeName.views.album_list'),
Run Code Online (Sandbox Code Playgroud)

(r"", SomeName.views.album_list.as_view()),
Run Code Online (Sandbox Code Playgroud)

记得改变SomeName:)