英文:
what i handel 500 (Internal server error) when url pattern str?
问题
这是我的代码:
project/urls/py:
from django.contrib import admin
from django.urls import path
from todoapp import views
urlpatterns = [
# 管理员页面
path('admin/', admin.site.urls),
# 主页
path('', views.index, name='todo'),
# 根据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>
当我故意访问例如 /del/abc
时,我得到了500错误,而不是我预期的404错误。所以真正的问题是什么?
英文:
This is my code:
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'
and 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>
when I intentionally access eg /del/abc then I get the 500 error instead of the 404 I expected
So what's the real problem here?
i tried replacing str to int and then i went to /del/abc got the page not found i expected
path('del/<int:item_id>', views.remove, name='del'),
答案1
得分: 1
The action attribute value of the form tag is incorrect. Try using the django built-in tag 'url' as instructed in the official documentation
英文:
The action attribute value of the form tag is incorrect. Try using the django built-in tag 'url' as instructed in the official documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论