[Answer]-When I catch the data in my views, how I clean the data?

1👍

When you access usr in a view, in your case, it will be a number as specified by your regular expression. Any requests to urls of the above format where the value of usr is not a number will receive a 404 page.

i.e.  /users/abcd/  will return 404

That being said, you’ll still likely want to validate usr in your view. For example, it can be inferred that you intend that the the usr variable should refer to an existing Django User. In that case, you’ll want to check to see if the user exists. Here is an example which will lookup a Django user based on the user id (assuming usr refers to a user id). It will return a user instance if one exists, or an 404 error page if one does not.

from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User

def this_view(request, usr):
    user = get_object_or_404(User, pk=usr)
    ...
👤Joe J

Leave a comment