我正在开发个人博客的后端,并创建了一个视图,用于删除帖子的单个标签。
views.py
var array1 = ["j1", "ff2", "3hj", "4sss", "5gh", "6ss", "7aqw"],
array2 = ["klp3", "jks32", "44sss", "3hj", "5gh", "6ss", "7aqw"];
array1.forEach( (value,index1) => {
if(array2.includes(value)){
var index2 = array2.indexOf(value);
console.log(value + " found index1:" + index1 + ", index2:" + index2);
}
});
models.py
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.text import slugify
from .forms import BlogTagForm
from .models import BlogTag
def deleteBlogTag(request, slug_tag):
if request.method == 'POST':
tag = BlogTag.objects.get(slug_tag=slug_tag)
tag.delete()
return redirect('tag_list')
urls.py
from django.db import models
from django.urls import reverse
class BlogTag(models.Model):
tag_name = models.CharField(
'Tag',
max_length=50,
help_text="Every key concept must be not longer then 50 characters",
unique=True,
)
slug_tag = models.SlugField(
'Slug',
unique=True,
help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents",
)
def __str__(self):
return self.tag_name
def get_absolute_url(self):
return reverse("single_blogtag", kwargs={"slug_tag": self.slug_tag})
class Meta:
ordering = ['tag_name']
tag_list.html
path("tags/", views.listTagAdmin, name='tag_list_admin'),
path("create-tag/", views.createBlogTag, name='create_tag'),
path("update-tag/<str:slug_tag>/", views.updateBlogTag, name='update_tag'),
path("delete-tag/<str:slug_tag>/", views.deleteBlogTag, name='delete_tag'),
问题在于无法删除列表中的每个对象,而只能删除第一个对象。即使我尝试删除最后一个对象,我也会删除第一个对象而不是最后一个对象。
我在哪里错了?
答案 0 :(得分:2)
您所有的模式形式都被命名为相同,因此它将选择一个。如果搜索特定的id
,它将选择一个。为了解决这个问题,您应该给modal
赋予不同的id
:
<tbody>
{% for tag in tag_list %}
<tr>
<td colspan="1">{{ tag.tag_name }}</td>
<td colspan="1"><a href="{{ tag.get_absolute_url }}">{{ tag.tag_blogpost.count }}</a></td>
<td colspan="1">
<div class="row justify-content-md-center">
<a class="btn btn-success btn-sm mx-1" href="{% url 'update_tag' slug_tag=tag.slug_tag %}">Update</a>
<button data-target="#deleteModal{{ tag.pk }}" class="btn btn-danger btn-sm mx-1" type="button" data-toggle="modal">Delete</button>
<div class="modal fade" id="deleteModal{{ tag.pk }}" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title text-center" id="deleteModalLabel">Delete Request</h2>
</div>
<div class="modal-body">
<h3>Are you sure to delete this tag?</h3>
<h1 class="py-4"><em><strong>{{ tag.tag_name }}</strong></em></h1>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-sm" data-dismiss="modal">No, don't do this</button>
<form action="{% url 'delete_tag' slug_tag=tag.slug_tag %}" method="POST">
{% csrf_token %}
<button class="btn btn-danger btn-sm" type="submit" name="button">Yes, delete it</button>
</form>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
因此,这里我们在id
的{{1}}上添加了后缀,并在<div class="modal fade" id="deleteModal{{ tag.pk }}" ...>
中使用了相同的id
。