【问题标题】:How can i style the form errors of the USerCreationForm in django?如何在 django 中设置 USerCreationForm 的表单错误样式?
【发布时间】:2020-12-28 01:08:00
【问题描述】:

我正在做一个项目,我使用 USerCreationForm 制作了一个注册表单,我想设置表单错误的样式,导致错误在我的模板中看起来很丑image of my form and how error looks like

所以我想要的是它不应该显示错误所在的字段,它应该直接显示错误。

我的注册表

<form action="{% url 'registration_pg' %}" method="post">
            {% csrf_token %}
            <div class="name-container">
                <!-- < id="first_name " class="form-control" name="first_name" placeholder="first name" required> -->
                {{form.first_name}}
                {{form.last_name}}
            </div>
            <div class="username-container form-inputs">
                {{form.username}}
            </div>
            <div class="student-info">
                {{form.std}}
                {{form.div}}
                {{form.rollno}}
            </div>
            <div class="user-fields">
                {{form.email}}
                {{form.password1}}
                {{form.password2}}
            </div>
            <div class="form-errors">
                {{form.errors}}
            </div>
            <button class="btn btn-block btn-info ripple-effect" type="submit" name="Submit" alt="sign in" style="background:#0277bd;">Submit</button>
        </form>

我的 forms.py 文件

class StudentCreationForm(UserCreationForm):
first_name = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'id': 'first_name', 'class': 'form-control', 'placeholder': 'first name'}))
last_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'id': 'last_name', 'class': 'form-control', 'placeholder': 'last name'}))
username = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'id': 'username', 'class': 'form-control', 'placeholder': 'username eg (jhon16)'}))
std = forms.IntegerField(widget=forms.NumberInput(attrs={'id': 'std', 'class': 'form-control stu-info', 'placeholder': 'std'}))
div = forms.CharField(max_length=1, widget=forms.TextInput(attrs={'id': 'div', 'class': 'form-control stu-info', 'placeholder': 'division'}))
rollno = forms.IntegerField(widget=forms.NumberInput(attrs={'id': 'rollno', 'class': 'form-control stu-info', 'placeholder': 'roll no'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'id': 'email', 'class': 'form-control', 'placeholder': 'email'}))
password1 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'id': 'password1', 'class': 'form-control', 'placeholder': 'password'}))
password2 = forms.CharField(max_length=100, widget=forms.PasswordInput(attrs={'id': 'password2', 'class': 'form-control', 'placeholder': 'confirm password'}))

class Meta:
    model = User
    fields = ['first_name', 'last_name', 'username', 'std', 'div', 'rollno', 'email', 'password1', 'password2']

【问题讨论】:

    标签: django django-models django-forms django-users


    【解决方案1】:

    我正在处理同样的问题,但我找到了解决方案。

            {% for field, error in form.errors.items %}
            <div class="alert alert-danger my-3 p-1" role="alert">
                {{ error|striptags }}
            </div>
            {% endfor %}
    

    首先,您必须使用 for 循环来访问每个字段及其错误消息。然后只插入变量错误并应用striptags(它会删除项目符号点)。

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2018-03-23
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多