Commit 121d6d28 by Егор Юганов

till lesson 25, till navigation menu

1 parent d3bc83a1
No preview for this file type
No preview for this file type
No preview for this file type
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-06-27 08:10
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('music', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='song',
name='is_favorite',
field=models.BooleanField(default=False),
),
]
No preview for this file type
......@@ -16,6 +16,7 @@ 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=250)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.song_title
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
body{
background: white url("images/background.jpg");
}
\ No newline at end of file
<img src="{{ album.album_logo }}">
<img src="{{ album.album_logo }}" height="42" width="42">
<h1>{{ album.album_title }}</h1>
<h3>{{ album.artist }}</h3>
<h2>{{ album.artist }}</h2>
<ul>
<!-- <ul>
{% for song in album.song_set.all %}
<li>{{ song.song_title }} - {{ song.file_type }}</li>
{% endfor %}
</ul>
\ No newline at end of file
</ul> -->
{% 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 }}"> <!-- auto-incremented each time it prints a song -->
<label for="song{{ forloop.counter }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="http://grumpygaycritic.co.uk/wp-content/uploads/2015/06/Crystal_Clear_action_bookmark.png" height="16" width="16">
{% endif %}
</label><br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
\ No newline at end of file
<!-- Loads the path to your static files -->
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'music/style.css' %}" />
{% if all_albums %}
<h3>Here are all my Albums</h3>
<ul>
{% for album in all_albums %}
<li><a href="{% url 'detail' album.id %}">{{ album.album_title }}</a></li>
<li><a href="{% url 'music:detail' album.id %}">{{ album.album_title }}</a></li>
{% endfor %}
</ul>
{% else %}
......
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
# /music/
url(r'^$', views.index, name='index'), # r - regular exp., ^ - start, $ - end. this is for /music/
# /muisic/71/
url(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
# /muisic/71/favorite/
url(r'^(?P<album_id>[0-9]+)/favorite/$', views.favorite, name='favorite'),
]
No preview for this file type
from django.http import Http404
from django.shortcuts import render
from .models import Album
from django.shortcuts import render, get_object_or_404
from .models import Album, Song
def index(request):
all_albums = Album.objects.all()
......@@ -8,8 +8,23 @@ def index(request):
return render(request, 'music/index.html', context)
def detail(request, album_id):
#try:
# album = Album.objects.get(pk=album_id)
#except Album.DoesNotExist:
# raise Http404("Album does not exist")
# Instead of the above:
album = get_object_or_404(Album, pk=album_id)
return render(request, 'music/detail.html', {'album': album})
def favorite(request, album_id):
album = get_object_or_404(Album, pk=album_id)
try:
album = Album.objects.get(pk=album_id)
except Album.DoesNotExist:
raise Http404("Album does not exist")
return render(request, 'music/detail.html', {'album': album})
\ No newline at end of file
selected_song = album.song_set.get(pk=request.POST['song'])
except (KeyError, Song.DoesNotExist):
return render(request, 'music/detail.html', {'album': album, 'error_message': "You did not select a valid song"})
else:
selected_song.is_favorite = True
selected_song.save()
return render(request, 'music/detail.html', {'album': album})
\ No newline at end of file
No preview for this file type
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!