1

i have a url :

path('admin-panel/users/update/<id>/',user_update_for_admin, name="user_update_for_admin"),

and the view for that url:

def user_update_for_admin(request,id):

    user = get_object_or_404(UsersForAdmin, id=id)

everything works fine but if an id is not provided in the url for exemple if i type :

admin-panel/users/update/

i got this error: Field 'id' expected a number but got 'update'. how do i fix that ?

1 Answer 1

3

Use a path converter [Django-doc] such that the URL will only "fire" in case it is an number (sequence of digits):

path(
    'admin-panel/users/update/<int:id>/',
    user_update_for_admin,
    name='user_update_for_admin'
),

In case you pass a non-int value, it will not "fire" and look for other paths. If no paths capture the url pattern, it will return a 404, which is probably the most sensical HTTP response.

You furthermore need to rewrite the path above to:

path(
    'admin-panel/users/<int:id>/',
    user_detail_for_admin,
    name='user_detail_for_admin'
)
Sign up to request clarification or add additional context in comments.

8 Comments

@NAS: is something else referring to the user_update_for_admin function? You did use <int:...>, right?
@NAS: at first sight it looks like you also have a path admin-panel/users/<id> (so where update matches with id).
@NAS: normally not. Are you sure the error is raised by the same view?
@NAS: normally the path admin-panel/users/update/ however should not trigger this view in the first place, since it requires at least one character for <id>.
@NAS: the path one line above it is the culprit, this needs an <int:...> as well. In fact, probably most (all) <id>s are better replaced by <int:id>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.