我为我的专辑'创建了DeleteView。模型,但它现在按预期工作。这是我的脚本:
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^albums/create/$', views.AlbumCreateView.as_view(), name='album_create'),
url(r'^albums/(?P<slug>[-\w]+)/update/$', views.AlbumUpdateView.as_view(), name='album_update'),
url(r'^albums/(?P<slug>[-\w]+)/delete/$', views.AlbumDeleteView.as_view(), name='album_delete'),
url(r'^albums/(?P<slug>[-\w]+)/$', views.AlbumDetailView.as_view(), name='album_detail'),
url(r'^albums/$', views.AlbumListView.as_view(), name='album_index'),
url(r'^songs/create/$', views.SongCreateView.as_view(), name='song_create'),
url(r'songs/$', views.SongListView.as_view(), name='song_index'),
url(r'^(?P<song_id>[0-9]+)/favorite/$', views.make_song_favorite, name='song_favorite'),
url(r'^$', views.AllAlbumListView.as_view(), name='index'),
]
views.py
from django.views.generic import ListView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Album
class AlbumListView(LoginRequiredMixin, ListView):
template_name = 'music/album_index.html'
context_object_name = 'all_albums'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['album_active'] = True
return context
def get_queryset(self):
queryset = Album.objects.filter(owner=self.request.user).order_by('-release_date')
return queryset
class AlbumDeleteView(DeleteView):
model = Album
template_name = 'music/album_index.html'
success_url = '/albums/'
album_index.html
{% extends "base.html" %}
{% block content %}
<div class="row">
<div class="col-md-9">
<h1>Your Albums</h1>
<a class="btn btn-primary" href="{% url 'music:album_create' %}">Add Album
<span class="oi oi-plus" title="Add New Album"></span>
</a>
<div class="card-columns" style="margin-top: 10px;">
{% for album in all_albums %}
<div class="card">
<img class="card-img-top img-fluid" src="{{ album.logo.url }}"
style="background-size: cover;">
<div class="card-body">
<h4 class="card-title">{{ album.title }}</h4>
<h6 class="card-subtitle mb-2 text-muted">{{ album.artist }}</h6>
<a class="btn btn-primary" href="{% url 'music:album_detail' album.slug %}">Look Inside</a>
<a class="btn btn-primary" href="{% url 'music:album_update' album.slug %}">
<span class="oi oi-pencil" title="Edit Album"></span>
</a>
<a class="btn btn-primary" href="{% url 'music:album_delete' album.slug %}"
data-toggle="modal" data-target="#albumDeleteConfirm">
<span class="oi oi-trash" title="Delete Album"></span>
</a>
<div class="modal fade" id="albumDeleteConfirm"
tabindex="-1" role="dialog" aria-labelledby="confirmAlbumDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirmAlbumDelete">Remove album '{{ album.title }}'</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Are you sure to remove this album? Please, note that all songs
associated with this album will be also removed.
</p>
</div>
<div class="modal-footer">
**<form method="post" action="{% url 'music:album_delete' album.slug %}">{% csrf_token %}
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" value="Confirm">Remove</button>
</form>**
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="col-md-3">
</div>
</div>
{% endblock %}
正如您在views.AlbumListView中看到的那样,我按照发布日期订购了相册。我的问题是,当我点击删除按钮时,DeleteView删除第一张专辑(即具有最新发行日期的专辑),而不是点击的专辑。我无法弄清问题在哪里。你能帮忙吗?
提前致谢!
答案 0 :(得分:1)
id
应该是唯一的,但每张专辑都有id="albumDeleteConfirm"
的div。
使ID唯一(例如,通过包含slug或pk),并更新data-target
。
在DeleteView
中,我不会使用您用于列表视图的相同模板music/album_index.html
。有一个示例删除模板in the docs。这个模板只是一个后备 - 如果你的模态有效,那么它将不会被看到。
答案 1 :(得分:1)
试试这个 我正在做同样的事情,我使用这个。 view.py
class AlbumDeleteView(DeleteView):
model = Album
success_url = reverse_lazy('music:index')
在url.py中
url(r'album/(?P<pk>[0-9]+)/delete/$', views.AlbumDeleteView.as_view(), name="album_delete")