我有一个页面,其中嵌套在我的表单中的列表中的项目可以被收藏,它会重定向回到应该对数据库进行更新的同一页面,然后它会在项目旁边显示一个星号。虽然这应该工作正常我的表格根本不会改变数据库。我相信它不是html渲染,因为当我手动更改管理面板中的字段时,它呈现得很好。是什么导致这不发布?
view.py:
<img src="{{album.album_logo}}">
<h1>{{album.album_title}} </h1>
<h2>{{album.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}}">
<span>{{song.song_title}}
{% if song.is_favorite %}
<img src="http://i.imgur.com/b9b13Rd.png" />
{% endif %}
</span>
</label><br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
from django.db import models
# Create your models here.
class Album(models.Model):
artist = models.CharField(max_length=255)
album_title = models.CharField(max_length=255)
genre = models.CharField(max_length=255)
album_logo = models.CharField(max_length=255)
def __str__(self):
return self.album_title + ' -- ' + self.artist
class Song(models.Model):
# on delete this deletes this entry
album = models.ForeignKey(Album, on_delete=models.CASCADE)
file_type = models.CharField(max_length=4)
song_title = models.CharField(max_length=255)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.song_title
model.py:
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
# /music/
url(r'^$', views.index, name='index'),
# /music/<album_id>/
url(r'^(\d+)/$', views.detail, name='detail'),
# logic for favoriting adjusts model and redirects to same place
# /music/<album_id>/favorite
url(r'^(\d+)/favorite/$', views.favorite, name='favorite'),
]
urls.py:
GetTable