Django study notes---form form

views.py

from django import forms
class FM(forms.Form):
    user = forms.CharField(error_messages={'required':'Username cannot be empty'}) # Customize the error message
    pwd = forms.CharField(max_length=12,min_length=6,error_messages={'required':"password cannot be empty","max_length":"password length is less than 12","min_length":"password length is greater than 6"} )
    email = forms.EmailField(error_messages={'required':'The email address cannot be empty','invalid':"The email format is incorrect"})

def fm(request):
    if request.method == "GET":
        return render(request,'fm.html')
    if request.method == "POST":
        obj = FM(request.POST)
        r1 = obj.is_valid() # True if the data in the form conforms to the specification, (the email is in the email format)
        if r1:
            print(obj.cleaned_data)
        else:
            print(obj.errors)   # <ul class="errorlist"><li>user<ul class="errorlist"><li>用户名不能为空</li></ul></li><li>pwd<ul class="errorlist"><li>密码长度大于6</li></ul></li><li>email<ul class="errorlist"><li>邮箱格式不对</li></ul></li></ul>
            print(obj.errors.as_json())   # {"user": [{"message": "\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a", "code": "required"}], "pwd": [{"message": "\u5bc6\u7801\u957f\u5ea6\u5927\u4e8e6", "code": "min_length"}], "email": [{"message": "\u90ae\u7bb1\u683c\u5f0f\u4e0d\u5bf9", "code": "invalid"}]}
            print(obj.errors['user']) # <ul class="errorlist"><li>Username cannot be empty</li></ul>
    return render(request,"fm.html",{'obj':obj})

fm.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/app1/fm/" method="POST">
        {% csrf_token %}
        <p><input type="text" name="user" />{{ obj.errors.user.0 }}</p> <!-- get the first error -->
        <p><input type="password" name="pwd" />{{ obj.errors.pwd.0 }}</p>
        <p><input type="text" name="email" />{{ obj.errors.email.0 }}</p>
        <input type="submit" value="提交" />
    </form>
</body>
</html>

This will enable form validation. However, as shown in the figure, there is a disadvantage that every time you click submit, the value in the input is gone.

So after improvement:

views.py

from django import forms
class FM(forms.Form):
    user = forms.CharField(error_messages={'required':'Username cannot be empty'}) # Customize the error message
    pwd = forms.CharField(max_length=12,min_length=6,error_messages={'required':"password cannot be empty","max_length":"password length is less than 12","min_length":"password length is greater than 6"} )
    email = forms.EmailField(error_messages={'required':'The email address cannot be empty','invalid':"The email format is incorrect"})

def fm(request):
    if request.method == "GET":
        obj = FM()
        return render(request,'fm.html',{'obj':obj}) # Pass the obj object to fm.html, otherwise the page will be displayed as empty
    if request.method == "POST":
        obj = FM(request.POST)
        r1 = obj.is_valid() # True if the data in the form conforms to the specification, (the email is in the email format)
        if r1:
            print(obj.cleaned_data)
            models.UserInfo.objects.create(**obj.cleaned_data) # This will save a record in the database
        else:
            print(obj.errors)   # <ul class="errorlist"><li>user<ul class="errorlist"><li>用户名不能为空</li></ul></li><li>pwd<ul class="errorlist"><li>密码长度大于6</li></ul></li><li>email<ul class="errorlist"><li>邮箱格式不对</li></ul></li></ul>
            print(obj.errors.as_json())   # {"user": [{"message": "\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a", "code": "required"}], "pwd": [{"message": "\u5bc6\u7801\u957f\u5ea6\u5927\u4e8e6", "code": "min_length"}], "email": [{"message": "\u90ae\u7bb1\u683c\u5f0f\u4e0d\u5bf9", "code": "invalid"}]}
    return render(request,"fm.html",{'obj':obj})



fm.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/app1/fm/" method="POST">
        {% csrf_token %}
        <p>{{ obj.user }}{{ obj.errors.user.0 }}</p> <!-- get the first error -->
        <p>{{ obj.pwd }}{{ obj.errors.pwd.0 }}</p>
        <p>{{ obj.email }}{{ obj.errors.email.0 }}</p>
        <input type="submit" value="提交" />
    </form>
</body>
</html>


To remove the required, the email becomes text, and it becomes a low-level browser mode, so that a custom error can be generated next to the input box. Because advanced browsers will come with required, otherwise the error message is like this



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570645&siteId=291194637