donner a une variable false ,la valeur true ?
Salut tout le monde :
j'ai cree un project django intitule "website",dans lequel il y a une application "music" .j'ai un album avec plusieur music , le but est quand je clique sur une music suivi du boutton favorite , un coeur doit apparaitre a cote de cette derniere . j'ai constate que mon code ne change pas la valeur de false en true , meme si , je suis sure que la logique est correcte . le model dans l'application music est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| from django.db import models
#Red pk 1
class Album(models.Model):
artist = models.CharField(max_length=250)
album_title=models.CharField(max_length=250)
genre=models.CharField(max_length=100)
album_logo=models.CharField(max_length=1000)
# ...
def __str__(self):
return self.album_title+'-'+self.artist
class Song(models.Model):
album =models.ForeignKey(Album,on_delete=models.CASCADE)
file_type=models.CharField(max_length=10)
song_title=models.CharField(max_length=100)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.song_title |
le file views.py dans l'application music est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from django.template import loader
from music.models import Album
#html=''
#for album in all_albums:
# url='/music/'+str(album.id)+'/'
# html+='<a href="'+url+'">'+album.album_title+'</a><br>'
def index(request):
all_albums=Album.objects.all()
# template = loader.get_template('music/index.html')
context={'all_albums':all_albums}
#return HttpResponse(template.render(context,request))
return render(request,'music/index.html',context)
def detail(request,album_id):
#try:
#album = Album.objects.get(pk=album_id)
#context={'album':album}
#except Album.DoesNotExist as err:
#raise Http404("Album does not exist")
album = get_object_or_404(Album, pk=album_id)
context={'album':album}
return render(request, 'music/detail.html', context)
def favorite(request,album_id):
album = get_object_or_404(Album, pk=4354)
try:
selected_song=album.song_set.get(pk=request.POST['song'])
print (selected_song)
except(KeyError,song.DoesNotExist):
return render(request, 'music/detail.html', {'album':album, 'error_message':"You did not select a valid song",})
selected_song.is_favorite = True
selected_song.save()
context={'album':album}
return render(request, 'music/detail.html', context) |
et puis le file detail.html qui fait voirr le resultat est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<script src="script.js"></script>
</head>
<body>
<img src="{{album.album_logo}}">
<h1>{{album.album_title}}</h1>
<h2>{{artist}}</h2>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}" method="post">
{% csrf_token %}
{% for song in album.song_set.all%}
<input type="radio" id="song{{forloop.counter}}" name="song" value="{{song.id}}">
<label for="song{{forloop.counter}}">
{{song.song_title}}-{{song.file_type}}
<span>{{song.is_favorite}}</span>
{% if song.is_favorite %}
<i class="fas fa-heart"></i>
{% endif %}
</label><br>
{% endfor %}
<input type="submit" value="favorite">
</form>
</body>
</html> |