Django: imported class is not being called

Asked

Viewed 23 times

0

Hey, guys, I created a view to return a HttpResponse simple importing a class from the models folder as follows:

from .models import Album

and then creating the view function:

def index(request):
   all_albums = Album.objects.all()
   html = ''
   for Album in all_albums:
       url = '/music/' + str(album.id) + '/'
       html += '<a href="' + url + '">' + album.album_title + '</a><br>'

   return HttpResponse(html)

but at various points in my code, specifically where the 'album' class is returning unresolved reference 'album' and also the Album class that was imported is not being used. However, both the class name and import call are correct as far as I have looked. Any solution or error you have seen?

1 answer

1


In the loop statement you are assigning to the class itself Album instead of the variable album.

Replace this line:

for Album in all_albums:

For this reason:

for album in all_albums:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.