我正在编写一个HTML页面,该页面将通过html表单和“ POST”方法接收用户的输入并将其提交到链接。链接被附加到一个视图,该视图根据用户传递的信息访问数据库条目。但是,当我在该HTML页面中包含导航栏标头的引导程序代码时,它将无法访问数据库。
我的查看功能是
def favorite(request,album_id):
album = get_object_or_404(Album,id = album_id)
try:
selected_song = album.song_set.get(id = request.POST['song'])
except:
return render(request,'music/detail.html', {'album':album,'error_message':'No song selected!!'})
else:
selected_song.isFavorite = True
selected_song.save()
return render(request,'music/detail.html',{'album':album})
HTML页面是-
{% extends 'music/base.html' %}
{% block body %}
{% if error_message %}
<p><strong>{{error_message}}</strong></p>
{% endif %}
<img src = "{{album.album_logo}}">
<h1>The album is {{album}} </h1>
<h2>Artist - {{album.artist}}</h2>
<h3>The songs in this album are: </h3>
<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.isFavorite == True %}
' fav '
{% endif %}
</label><br>
{% endfor %}
<input type = 'submit' value = "Favorite">
</form>
</body>
</html>
而基本模板代码是'music / base.html'-
<html>
<head>{% load staticfiles %}
<title>
{% block title %}
{% endblock %}
</title>
<link rel = 'stylesheet' type = 'text/css' href = "{% static
'music/style.css' %}">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-
Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>
<body>
<nav class = "navbar navbar-inverse">
<div class = "container-fluid">
<div class = "navbar - header">
<a class = "navbar-brand" href = "{%url 'music:index' %}">Vibe</a>
<button type = "button" class = "navbar-toggle" data-toggle = "collapse" data-target = "#topNavBar">
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
</div>
<div class = "collapse navbar-collapse" id = "topNavBar">
<ul class = "nav navbar-nav">
<li class = "">
<a href = "{% url 'music:index' %}">
<span class = "glyphicon-cd" aria-hidden = "true"></span> Albums
</a></li>
<li class = "">
<a href = "{% url 'music:index' %}">
<span class = "glyphicon-cd" aria-hidden = "true"></span> Songs
</a>
</li>
<form class = "navbar-form navbar-left" role = "search" method = "get"
action
= "#">
<div class = "form-group">
<input type = "text" class = "form-control" name = "q" value = "">
</div>
<button type = "submit" class = "btn btn-default">Search</button>
<ul class = 'navbar-nav navbar-right'>
<li class = "">
<a href = "#">
<span class = "glyphicon-power" aria-hidden = "true"></span> Add
Album</span>
</a>
</li>
<li class = "">
<a href = "#">
<span class = "glyphicon-power" aria-hidden = "true"></span>
Logout</span>
</a>
</li>
</div>
</div>
</nav>
{% block body %}
{% endblock %}
</body>
</html>
我得到与错误消息相对应的错误消息(在顶部)(最喜欢的函数),但是当我从代码中删除引导程序时,没有得到这样的错误。 附言错误消息是
try:
selected_song = album.song_set.get(id =
request.POST['song'])
except:
return render(request,'music/detail.html',
{'album':album,'error_message':'No song selected!!'}