ValueError: Поле 'song_id' ожидало число, но получило 'Удалить из избранного'
ValueError: Поле 'song_id' ожидало число, но получило 'Remove from Favourites'. Django Music web App: Я добавил функцию "Добавить в избранное", но не могу добавить функцию "Удалить из избранного"
Favourite.html
<form action="/music/favourites" method="POST">{% csrf_token %}
<input type="hidden" name="video_id" value="{{song.song_id}}">
<button type="submit" class="btn btn-outline-danger">Add to Favourite</button>
</form>
<form method="post">
{% csrf_token %}
<input type="submit" class="btn btn-outline-danger" value="Remove from Favourites"
name={{song.id}} >
</form>
#models.py
class Song(models.Model):
song_id = models.AutoField(primary_key= True)
name = models.CharField(max_length= 2000)
singer = models.CharField(max_length= 2000)
tags = models.CharField(max_length= 100)
image = models.ImageField(upload_to = 'docs')
song = models.FileField(upload_to= 'docs')
movie = models.CharField(max_length = 150, default = "None")
def __str__(self):
return self.name
class Favourites(models.Model):
watch_id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
video_id = models.CharField(max_length=10000000, default="")
#Views.py
def favourites(request):
if request.method == "POST":
user = request.user
video_id = request.POST['video_id']
fav = Favourites.objects.filter(user=user)
for i in fav:
if video_id == i.video_id:
message = "Your Video is Already Added"
break
else:
favourites = Favourites(user=user, video_id=video_id)
favourites.save()
message = "Your Video is Succesfully Added"
song = Song.objects.filter(song_id=video_id).first()
return render(request, f"music/songpost.html", {'song': song, "message": message})
wl = Favourites.objects.filter(user=request.user)
ids = []
for i in wl:
ids.append(i.video_id)
preserved = Case(*[When(pk=pk, then=pos) for pos, pk in enumerate(ids)])
song = Song.objects.filter(song_id__in=ids).order_by(preserved)
return render(request, "music/favourite.html", {'song': song})
Эта функция неправильная, но я не могу понять
def remove_from_favourite(request, song_id):
if request.method == "POST":
query = Song.objects.filter(id=song_id).first()
query.delete()
messages.success(request, "Removed from favorite!")
return redirect('favourite', song_id=song_id)
Я бы рекомендовал изменить HTML следующим образом
<input type="submit" class="btn btn-outline-danger" value="{{song.id}}"
name={{song.id}} >