Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting Field 'None' expected a number but got 'update'. when using UpdateView in Django

I am using UpdateView for my update page, but I get this error:
Field ‘None’ expected a number but got ‘update’.

my urls.py looks like this:

path('post/<pk>/update/', PostUpdateView.as_view(), name='post_update'),

Everything works fine if I change my update URL to anything other than "post". For example, this works fine:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

path('abcxyz/<pk>/update/', PostUpdateView.as_view(), name='post_update'),

I would like to use the word "post" in URL, because i also use it in the other URLs and they work just fine (except for delete view, which gives me the same error). Is there any way to fix this error without changing the URL?

Note: It has worked before, but it broke at some point when I was trying to use django-PWA. I’m not sure if this is related though.

Thank you for your help!

>Solution :

This is likely because you have another path like:

path('post/update/update/', PostUpdateView.as_view(), name='post_update'),

or something similar. It is possible that one or both update/s are URL parameters.

If you then for example visit post/update/update, and your PostUpdateView is defined first, the it will trigger the view with pk='update'.

If the primary key is simply a number, you can make use of the <int:…> path converter to prevent triggering the view when pk is something else than a sequence of digits:

path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post_update'),

EDIT: Based on your comment, there was another view before this one with:

path('post/<year>/<month>/', FilteredPostListView.as_view(), name='filtered_post')

If one thus visits post/123/update, it will trigger the FilteredPostListView with 123 as year, and update as month, and hence it will raise an error.

You can put the path below that of the ProductUpdateListView, and you furthermore might want to use the <int:…> as well to only trigger the view when both parameters are sequences of digits:

path('post/<int:year>/<int:month>/', FilteredPostListView.as_view(), name='filtered_post')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading