couz2200
couz2200

Reputation: 31

what i handel 500 (Internal server error) when url pattern str?

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'),

Upvotes: 0

Views: 119

Answers (1)

Hovak
Hovak

Reputation: 11

The action attribute value of the form tag is incorrect. Try using the django built-in tag 'url' as instructed in the official documentation

Upvotes: 1

Related Questions