我在更新视图和删除视图上遇到麻烦。代码下方:
views.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 updateBlogTag(request, slug_tag=None):
update_tag = get_object_or_404(BlogTag, slug_tag=slug_tag)
form = BlogTagForm(request.POST or None, instance=update_tag)
if form.is_valid():
update_tag = form.save(commit=False)
update_tag.slug_tag = slugify(update_tag.tag_name)
update_tag.save()
return redirect('tag_list')
context = {
'form': form,
}
template = 'blog/editing/create_tag.html'
return render(request, template, context)
def deleteBlogTag(request, slug_tag):
if request.method == 'POST':
tag = BlogTag.objects.get(slug_tag=slug_tag)
tag.delete()
return redirect('tag_list')
models.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']
forms.py
from django import forms
from .models import BlogTag
class BlogTagForm(forms.ModelForm):
tag_name = forms.CharField(
max_length=50,
help_text="<small>Write a tag here. The tag must be have max 50 characters.</small>",
widget=forms.TextInput(
attrs={
"placeholder": "Tag",
"type": "text",
"id": "id_tag",
"class": "form-control form-control-lg",
}
),
)
class Meta:
model = BlogTag
fields = ["tag_name"]
urls.py
path("update-tag/", views.updateBlogTag, name='update_tag'),
path("delete-tag/", views.deleteBlogTag, name='delete_tag'),
tag_list.html
<table class="table table-striped shadow">
<thead>
<tr>
<th>Tag</th>
<th>Related Posts</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
{% for tag in tag_list %}
<tr>
<td>{{ tag.tag_name }}</td>
<td><a href="{{ tag.get_absolute_url }}">{{ tag.tag_blogpost.count }}</a></td>
<td>
<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 class="btn btn-danger btn-sm mx-1" type="button" data-toggle="modal" data-target="#deleteModal">Delete</button>
<div class="modal fade" id="deleteModal" 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' 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>
{% endfor %}
</tbody>
</table>
我已经将相同类型的代码用于其他视图而没有问题,但是在这种情况下,我遇到了这个错误:
在/ blog / tags / 上的更新视图 NoReverseMatch
使用关键字参数'{'slug_tag'的'update_tag'反向: 找不到'altro-tag'}'。尝试了1种模式:['blog / update \ -tag / $']
删除视图 / blog / tags /
上的NoReverseMatch找不到带有参数'('altro-tag',)'的'delete_tag'的反向字符。 1个 尝试过的模式:['博客/删除\-标签/ $']
我不知道我的代码中的错误在哪里。有人可以告诉我错误在哪里?
答案 0 :(得分:0)
该错误即将到来是因为您在构建URL时未添加尖括号来捕获子弹。我假设您的网址看起来像这样url(r'blog/update-tag/$', updateBlogTag(), name='updated_tag')
和url(r'blog/delete-tag/$', deleteBlogTag(), name='delete_tag')
,而他们应该看起来像这样url(r'blog/update-tag/<slug:slug_tag>/$', updateBlogTag(), name='updated_tag')
和url(r'blog/delete-tag/<slug:slug_tag>/$', deleteBlogTag(), name='delete_tag')
reference