Django Streaming

Viewed 73

I just started with django and I want to make a spotify clone. I want to make it so then when you click on an album it shows the songs for that album.

this is my models.py

from django.db import models

    class album(models.Model):
        title = models.CharField(max_length=50)
        artist = models.IntegerField()
        genre = models.CharField(max_length=20)
        id = models.AutoField(primary_key=True)
        artwork = models.CharField(max_length=1000)
    
    
    class artist(models.Model):
        name = models.CharField(max_length=50)
        id = models.AutoField(primary_key=True)
    
    class song(models.Model):
        name = models.CharField(max_length=60)
        artist = models.IntegerField()
        album = models.IntegerField()
        id = models.AutoField(primary_key=True)

This is my views.py

from django.shortcuts import render
from .models import album, song

def home(request):
    context = {
        'albums': album.objects.all(),

       
    }
    return render(request, 'main/index.html', context) 
1 Answers

For that particular album for which the button is clicked, or all albums, please clarify your question.

Well, it cane achieved through frontend using dropdowns and much more components, or simply redirecting to another page which will show relevant albums.

Through django, if you need to see all albums so you are doing correct.

But if you see a particular album by clicking, so you have to pass that album's id or name or you can pass anything which is unique.

urls.py

path('particular-album/<int:id>/,views.particular_album,name='particular')

Html file

{% for album in albums %}
<a href="{% url 'particular' album.id %}">see particular album</a>

Views.py

def particular_album(req,id):
    one_album=album.objects.get(id=id)
    return render(req,"main/particular.html",{'one':one_album})

Then in your template file without running the loop you can get all the properties of this object.

If there are more than one objects, so you can use album.objects.filter(field_name=variable) same as like one object, but it gives you more than one, and the variable you have to pass must not be associated with unique constraint.So, it gives you all objects which belong to particular category.Then, you have to run loop in your template file.

Related