简体   繁体   English

Python - Django - 输入验证

[英]Python - Django - Input validation

How can I make a validation for post fetch input?如何验证后提取输入? I manually validate if the first digit has empty space or isalnum to make update operation as below but I wonder is there a better approach?我手动验证第一个数字是否有空格或 isalnum 来进行如下更新操作,但我想知道有没有更好的方法?

def update_user_meta(request):
    if request.method == "POST":
    name = request.POST.get("name")
    if name is not None and name[0:1].isalnum() == True:
            selectedUser.name = name
    else:
            selectedUser.name = selectedUser.name

You should use django forms https://docs.djangoproject.com/en/3.2/topics/forms/ .您应该使用 django 表单https://docs.djangoproject.com/en/3.2/topics/forms/

Unless this view is an API then use the serializers from the django rest framework.除非此视图是 API,否则请使用 django rest 框架中的序列化程序。https://www.django-rest-framework.org/api-guide/serializers/https://www.django-rest-framework.org/api-guide/serializers/

There are built-in functions to make field validations.有内置函数来进行字段验证。 It can be done with Regex Validator.可以使用正则表达式验证器来完成。

List of them are in the documents.他们的名单在文件中。 https://docs.djangoproject.com/en/3.2/ref/validators/ https://docs.djangoproject.com/en/3.2/ref/validators/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM