Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想以我想要的方式管理在html页面上django表单中收到的错误_Django_Django Forms_Django Templates - Fatal编程技术网

我想以我想要的方式管理在html页面上django表单中收到的错误

我想以我想要的方式管理在html页面上django表单中收到的错误,django,django-forms,django-templates,Django,Django Forms,Django Templates,已通过一些验证创建了一个。我想在HTML上正确显示错误,例如,如果我收到密码字段的错误,我想在该字段前面显示它。在我收到表单上的错误后,我正在共享我的代码以及页面的屏幕截图。简而言之,我希望以我想要的方式管理HTML上的表单错误 forms.py class UsersForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput,validators=[validators.MinLengthVa

已通过一些验证创建了一个。我想在HTML上正确显示错误,例如,如果我收到密码字段的错误,我想在该字段前面显示它。在我收到表单上的错误后,我正在共享我的代码以及页面的屏幕截图。简而言之,我希望以我想要的方式管理HTML上的表单错误

forms.py

class UsersForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput,validators=[validators.MinLengthValidator(8),])
    password_1 = forms.CharField(widget=forms.PasswordInput,label='Confirm Password')    
    class Meta:
        model = Users
        fields = ('username','first_name','last_name','date_of_birth')
        labels = {
            'date_of_birth': ('D.O.B'),
        }
        widgets = {
            'date_of_birth': DateInput(attrs={'type': 'date'})
        }
views.py

def create_user(request):
    if request.method == 'POST':
        form = UsersForm(request.POST)
        u = User.objects.get(username=request.user.get_username())
        if request.POST.get('password') != request.POST.get('password_1'):
            messages.error(request,'password mismatch, try again')
            return render(request,'login.html',{'form':form})         
        elif form.is_valid():
            cUser = Users.objects.create(username=form.cleaned_data['username'],
                first_name=form.cleaned_data['first_name'],
                last_name=form.cleaned_data['last_name'],
                password=make_password(form.cleaned_data['password']),
                date_of_birth=form.cleaned_data['date_of_birth'],
                created_by= u,)
            cUser.save()
        else:
            return render(request,'login.html',{'form':form})            
    form = UsersForm()
    context = {"form":form}
    return render(request,'login.html',context)
login.html

    <!DOCTYPE html>
    <html>
    <body>
        <pre><center>
            <h1>User Creation Form</h1>
            <form action = '' method="post">
                {% csrf_token %}    
            <table>
                {{ form.as_table }}              
            </table>  
            <input type="submit" name="register">               
            <h3>  
                {% for message in messages %}
                {{ message }}
                {% endfor %}
            </h3>
        </form>   
        </center></pre>
    </body>
    </html>

用户创建表单
{%csrf_令牌%}
{{form.as_table}}
{消息%中的消息为%s}
{{message}}
{%endfor%}

这是一个完整的文档(注意:向下滚动一点关于渲染错误的部分)。我投票将这个问题作为主题外的问题结束,因为这是一个完整的示例文档。首先看看是否足够(然后可以根据需要使用这些类设置div的样式)。如果没有,那么不要使用
{{form.as_table}}
而是更精细的输出,通过分别为每个字段编写{{form.field}、{{form.field.label}和{{form.field.errors}(如果需要特殊处理,可以在字段中循环,也可以对每个字段执行),您肯定希望将密码验证移到表单中-这也有文档记录()