在django中提交表单时出错

时间:2017-06-05 04:55:15

标签: python django

即使在本网站上回答相关问题,我也无法解决以下问题。

目前我正在做Django教程但却陷入了困境。以下错误,我得到了一首最喜欢的歌曲,这让我烦恼:

OperationalError at /music/2/
 {% for song in album.song_set.all %}Line 10

并用谷歌搜索寻找答案,但我似乎无法修复它。我感谢任何帮助。

<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 }}
            {% if song.is_favorite %}
                <img src="http://http://i.imgur.com/b9b13Rd.png" />
            {% endif %}
        </label><br>
    {% endfor %}
    <input type="submit" value="Favorite">
</form>

def favorite(request, album_id):
    album = get_object_or_404(Album, pk = album_id)
    try:
        selected_song = album.song_set.get(pk= int(request.POST['song']))
    except (KeyError, Song.DoesNotExist):
        return render(request, 'music/detail.html', {
            'album':album,
            'error_message': "You didn't select a valid song"
            })
    else:
        selected_song.is_favorite = True
        selected_song.save()
        return render(request, 'music/detail.html', {'album': album})

0 个答案:

没有答案