boyenec
boyenec

Reputation: 1617

Django Field 'id' expected a number but got '.....'. getting this error if forward slash missing from end of url

I am facing this error after added delete function in my views. if forward slash missing from end of any url like this "http://127.0.0.1:8000/blog" then I am getting this error Field 'id' expected a number but got 'blog'. If I type http://127.0.0.1:8000/blog/ then it taking to me blog page. For example I am trying to access my admin page like this http://127.0.0.1:8000/admin then it will give me same error Field 'id' expected a number but got 'admin'. This error happening for my all url. After adding this delete function in my views I am getting this error:

def DeleteNotificaton(request,noti_id):
    user= request.user

    if user.is_authenticated:
        author = Blog.objects.filter(author=user)
        if not author:
            Notifications.objects.filter(id=noti_id,receiver=user).delete()
            Notifications.objects.filter(id=noti_id,sender=user).delete()
            messages.add_message(request, messages.INFO, 'Notification deleted sucessfully.')
            return redirect('notifications:notify')
        if author:
         Notifications.objects.filter(id=noti_id,receiver=user).delete()
         messages.add_message(request, messages.INFO, 'Notification deleted sucessfully.')
         return redirect('notifications:notify_author')
    
    return redirect('http://127.0.0.1:8000/admin/')

how to solve this any idea??

my urls.py

from django.urls import path,include
from notifications import views
from .views import *
app_name = 'notifications'
urlpatterns = [
  
    path('notifications/',views.ShowNOtifications,name='notify'),
    path('author-notifications/',views.ShowAuthorNOtifications,name='notify_author'),
    path('<noti_id>',views.DeleteNotificaton, name='delete-noti'),
     
   
 
]

console error:

ValueError: Field 'id' expected a number but got 'blog'.
[25/Jul/2021 23:51:21] "GET /blog HTTP/1.1" 500 134098

Upvotes: 1

Views: 229

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

This is the result of defining the URLs that way. You first match with <noti_id> and since noti_id is an arbitrary string, it thus matches with /blog and will call the DeleteNotificaton view bith 'blog' as noti_id.

You can make your patterns more restrictive such that pk only accepts numbers by working with the <int:…> path converter:

from django.urls import path,include
from notifications import views
from .views import *

app_name = 'notifications'

urlpatterns = [  
    path('notifications/',views.ShowNOtifications,name='notify'),
    path('author-notifications/',views.ShowAuthorNOtifications,name='notify_author'),
    path('<int:noti_id>',views.DeleteNotificaton, name='delete-noti'),
]

Note: Section 9 of the HTTP protocol specifies that requests like GET and HEAD should not have side-effects, so you should not change entities with such requests. Normally POST, PUT, PATCH, and DELETE requests are used for this. In that case you make a small <form> that will trigger a POST request, or you use some AJAX calls.

Upvotes: 1

Related Questions