我如何处理 URL 模式字符串时出现 500(内部服务器错误)?

huangapple go评论73阅读模式
英文:

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(&#39;admin/&#39;, admin.site.urls),
    # home page
    path(&#39;&#39;, views.index, name=&#39;todo&#39;),
    # delete with item_id
    path(&#39;del/&lt;str:item_id&gt;&#39;, views.remove, name=&#39;del&#39;),
]

handler404 = &#39;todoapp.views.handling404&#39;

and app/views.py:

def remove(request, item_id):
    
    item = get_object_or_404(Todo, id=item_id)
    item.delete()

    messages.info(request, &quot;Item Removed!!!&quot;)
    return redirect(&#39;todo&#39;)

index.html:

&lt;form action=&quot;/del/{{item.id}}&quot; method=&quot;POST&quot; style=&quot;padding-right: 4%; padding-bottom: 3%;&quot;&gt;
                        {% csrf_token %}
                        &lt;button value=&quot;Remove&quot; type=&quot;submit&quot; class=&quot;btn btn-primary&quot; style=&quot;float: right;&quot;&gt;
                            &lt;span class=&quot;glyphicon glyphicon-trash&quot;&gt;    Remove&lt;/span&gt;
                        &lt;/button&gt;
                    &lt;/form&gt;

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(&#39;del/&lt;int:item_id&gt;&#39;, views.remove, name=&#39;del&#39;),

答案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

huangapple
  • 本文由 发表于 2023年6月15日 08:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定