这是我的代码:project/urls/py:
from django.contrib import admin
from django.urls import path
from todoapp import views
urlpatterns = [
# admin page
path('admin/', admin.site.urls),
# home page
path('', views.index, name='todo'),
# delete with item_id
path('del/<str:item_id>', views.remove, name='del'),
]
handler404 = 'todoapp.views.handling404'
和app/views.py:
def remove(request, item_id):
item = get_object_or_404(Todo, id=item_id)
item.delete()
messages.info(request, "Item Removed!!!")
return redirect('todo')
index.html:
<form action="/del/{{item.id}}" method="POST" style="padding-right: 4%; padding-bottom: 3%;">
{% csrf_token %}
<button value="Remove" type="submit" class="btn btn-primary" style="float: right;">
<span class="glyphicon glyphicon-trash"> Remove</span>
</button>
</form>
当我故意访问eg /del/abc时,我得到了500错误,而不是我预期的404,那么这里真实的的问题是什么?
我试着将str替换为int,然后我去了/del/abc,没有找到我期望的path('del/<int:item_id>', views.remove, name='del'),
页面。
1条答案
按热度按时间yvt65v4c1#
表单标记的操作属性值不正确。按照官方文档中的说明,尝试使用Django内置标签'url'