I
I
Ilya Atabekov2021-07-12 09:36:15
Django
Ilya Atabekov, 2021-07-12 09:36:15

How to pass two tables from models.py to html template?

views.py

def music(request):
    album = Music.objects.all()
    context = {"album": album}
    return render(request, 'main/music.html', context=context)


models.py

class Music(models.Model):
    name = models.CharField(max_length=60, verbose_name="название альбома")
    year = models.CharField(max_length=60, verbose_name="год выпуска", blank=True, default='')
    img = models.ImageField(verbose_name="картинка", upload_to="media/imgs", null=True, default='')
    music = models.CharField(max_length=60, verbose_name="ссылка", default='')

    def __str__(self):
        return self.name


class Music2(models.Model):
    name = models.CharField(max_length=60, verbose_name="название альбома 2 колонки")
    year = models.CharField(max_length=60, verbose_name="год выпуска", blank=True, default='')
    img = models.ImageField(verbose_name="картинка", upload_to="media/imgs", null=True, default='')
    music = models.CharField(max_length=60, verbose_name="ссылка", default='')

    def __str__(self):
        return self.name


You need to transfer 2 tables from models.py Music and Music2 to the template in the views.py file
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2021-07-12
@AlexNest

The same as one, through the context:

def music(request):
    album = Music.objects.all()
    album2 = Music2.objects.all()

    context = {"album": album,"album2": album2}
    return render(request, 'main/music.html', context=context)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question