Question

I am very new to django, I am trying to display the list of albums i have in my database. This is the Album model

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

My view is

    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()

I am able to add albums from the admin interface, but on going to the /albums/ url to display them, I get init() takes exactly 1 argument (2 given) error.

The template I am using

    {% 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 %}

I have gone through the answers to similar questions already asked here, but couldn't get the code to work.

Était-ce utile?

La solution

Usually this is because you forgot to put .as_view() in your urls.py:

Instead of

(r"", 'SomeName.views.album_list'),

put

(r"", SomeName.views.album_list.as_view()),

Remember to change SomeName :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top