django 删除 post noReverseMatch
django delete post noReverseMatch
正在尝试删除 post,但不知何故得到了 NoReverseMatch
views.py
@login_required
def task_detail(request, slug):
'''
Detailed view of all tasks on given project
'''
context = {}
checklist = get_object_or_404(Checklist, slug=slug)
context.update({'checklist':checklist})
form = NotesForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
print("\n\n for is valid")
author = Profile.objects.get(user=request.user)
new_note = form.save(commit=False)
new_note.user = author
new_note.checklist = checklist
new_note.save()
return redirect('task_detail', slug=slug)
context.update({
'form': form,
'title': checklist,
})
return render(request, 'projects/checklist.html', context)
@login_required
def delete_notes(request, note_id = None):
del_note = Note.objects.get(id = note_id)
del_note.delete()
return redirect('teams')
我的urls.py
urlpatterns = [
path('projects/', teams, name='teams'),
path('projects/project/<slug>/', projects, name='projects'),
path('projects/tasks/<slug>/', project_detail, name='project_detail'),
path('projects/checklist/<slug>/', task_detail, name='task_detail'),
path('projects/checklist/delete_notes/', delete_notes, name='delete_notes'),
]
在 html 中,我只有一个带有 url 的 href 要删除
<a class="dropdown-item text-danger" href="{% url 'delete_notes' notes.id %}">Delete</a>
得到;对 'delete_notes' 进行反转,未找到参数“(14,)”。尝试了 1 种模式:['projects/checklist/delete_notes/\Z']
不太确定还缺少什么,以为清单中的 slug 已经通过了?
重定向更新
urlpatterns = [
path('projects/', teams, name='teams'),
path('projects/project/<slug>/', projects, name='projects'),
path('projects/tasks/<slug>/', project_detail, name='project_detail'),
path('projects/checklist/<slug>/', task_detail, name='task_detail'),
path('projects/checklist/delete_notes/<int:note_id>/<slug:slug>/', delete_notes, name='delete_notes'),
@login_required
@require_http_methods(['post', 'delete'])
def delete_notes(request, note_id, slug):
'''
Delete given note
'''
del_note = Note.objects.filter(id=note_id).delete()
return redirect('task_detail', slug=slug)
更新了 slug
在html
<form method="POST" action="{% url 'delete_notes' notes.id title.slug %}">
{% csrf_token %}
<a class="dropdown-item">
<button type="submit"class="btn btn-danger">Delete</button>
</a>
</form>
您应该指定一个参数放置 notes.id
参数的位置,例如:
path('projects/checklist/delete_notes/<strong><int:note_id></strong>/', delete_notes, name='delete_notes'),
但是,此类视图只能通过 POST 或 DELETE 请求触发,而不能通过 GET 请求触发:GET 请求只能用于 检索 数据,不要改变实体。
因此您可以通过以下方式保护视图:
from django.views.decorators.http import <strong>require_http_methods</strong>
@login_required
<strong>@require_http_methods(['POST', 'DELETE'])</strong>
def delete_notes(request, note_id):
Note.objects.filter(id=note_id).delete()
return redirect('teams')
并且在模板中您应该使用“mini-form”:
<form <strong>method="post"</strong> <strong>action="{% url 'delete_notes' notes.id %}</strong>">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
如果你想重定向到给定任务的task_detail
,你可以添加一个额外的参数:
path('projects/checklist/delete_notes/<strong><int:note_id></strong>/<strong><slug:slug></strong>/', delete_notes, name='delete_notes'),
并重定向:
from django.views.decorators.http import <strong>require_http_methods</strong>
@login_required
<strong>@require_http_methods(['POST', 'DELETE'])</strong>
def delete_notes(request, note_id, <b>slug</b>):
Note.objects.filter(id=note_id).delete()
return redirect('task_detail', <b>slug=slug</b>)
那么在表单中你应该传递任务的 slug:
<form method="post" action="{% url 'delete_notes' notes.id <b>task.slug</b> %}">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
正在尝试删除 post,但不知何故得到了 NoReverseMatch
views.py
@login_required
def task_detail(request, slug):
'''
Detailed view of all tasks on given project
'''
context = {}
checklist = get_object_or_404(Checklist, slug=slug)
context.update({'checklist':checklist})
form = NotesForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
print("\n\n for is valid")
author = Profile.objects.get(user=request.user)
new_note = form.save(commit=False)
new_note.user = author
new_note.checklist = checklist
new_note.save()
return redirect('task_detail', slug=slug)
context.update({
'form': form,
'title': checklist,
})
return render(request, 'projects/checklist.html', context)
@login_required
def delete_notes(request, note_id = None):
del_note = Note.objects.get(id = note_id)
del_note.delete()
return redirect('teams')
我的urls.py
urlpatterns = [
path('projects/', teams, name='teams'),
path('projects/project/<slug>/', projects, name='projects'),
path('projects/tasks/<slug>/', project_detail, name='project_detail'),
path('projects/checklist/<slug>/', task_detail, name='task_detail'),
path('projects/checklist/delete_notes/', delete_notes, name='delete_notes'),
]
在 html 中,我只有一个带有 url 的 href 要删除
<a class="dropdown-item text-danger" href="{% url 'delete_notes' notes.id %}">Delete</a>
得到;对 'delete_notes' 进行反转,未找到参数“(14,)”。尝试了 1 种模式:['projects/checklist/delete_notes/\Z']
不太确定还缺少什么,以为清单中的 slug 已经通过了?
重定向更新
urlpatterns = [
path('projects/', teams, name='teams'),
path('projects/project/<slug>/', projects, name='projects'),
path('projects/tasks/<slug>/', project_detail, name='project_detail'),
path('projects/checklist/<slug>/', task_detail, name='task_detail'),
path('projects/checklist/delete_notes/<int:note_id>/<slug:slug>/', delete_notes, name='delete_notes'),
@login_required
@require_http_methods(['post', 'delete'])
def delete_notes(request, note_id, slug):
'''
Delete given note
'''
del_note = Note.objects.filter(id=note_id).delete()
return redirect('task_detail', slug=slug)
更新了 slug
在html
<form method="POST" action="{% url 'delete_notes' notes.id title.slug %}">
{% csrf_token %}
<a class="dropdown-item">
<button type="submit"class="btn btn-danger">Delete</button>
</a>
</form>
您应该指定一个参数放置 notes.id
参数的位置,例如:
path('projects/checklist/delete_notes/<strong><int:note_id></strong>/', delete_notes, name='delete_notes'),
但是,此类视图只能通过 POST 或 DELETE 请求触发,而不能通过 GET 请求触发:GET 请求只能用于 检索 数据,不要改变实体。
因此您可以通过以下方式保护视图:
from django.views.decorators.http import <strong>require_http_methods</strong>
@login_required
<strong>@require_http_methods(['POST', 'DELETE'])</strong>
def delete_notes(request, note_id):
Note.objects.filter(id=note_id).delete()
return redirect('teams')
并且在模板中您应该使用“mini-form”:
<form <strong>method="post"</strong> <strong>action="{% url 'delete_notes' notes.id %}</strong>">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
如果你想重定向到给定任务的task_detail
,你可以添加一个额外的参数:
path('projects/checklist/delete_notes/<strong><int:note_id></strong>/<strong><slug:slug></strong>/', delete_notes, name='delete_notes'),
并重定向:
from django.views.decorators.http import <strong>require_http_methods</strong>
@login_required
<strong>@require_http_methods(['POST', 'DELETE'])</strong>
def delete_notes(request, note_id, <b>slug</b>):
Note.objects.filter(id=note_id).delete()
return redirect('task_detail', <b>slug=slug</b>)
那么在表单中你应该传递任务的 slug:
<form method="post" action="{% url 'delete_notes' notes.id <b>task.slug</b> %}">
{% csrf_token %}
<button type="submit">Delete</button>
</form>