django - When I catch the data in my views, how I clean the data? -
example:
url(r'^/users/(?p<usr>[0-9]+)/$', 'home.views.who_user'),
how have clean usr in view?
when access usr in view, in case, number specified regular expression. requests urls of above format value of usr not number receive 404 page.
i.e. /users/abcd/ return 404
that being said, you'll still want validate usr in view. example, can inferred intend the usr variable should refer existing django user. in case, you'll want check see if user exists. here example lookup django user based on user id (assuming usr refers user id). return user instance if 1 exists, or 404 error page if 1 not.
from django.shortcuts import get_object_or_404 django.contrib.auth.models import user def this_view(request, usr): user = get_object_or_404(user, pk=usr) ...
Comments
Post a Comment