簡體   English   中英

如何在 Django 中設置 USerCreationForm 的表單錯誤樣式?

[英]How can i style the form errors of the USerCreationForm in django?

我正在處理一個項目,我使用 USerCreationForm 制作了一個注冊表單,我想為表單錯誤設置樣式,因為我的表單模板圖像中的錯誤看起來非常難看,以及錯誤的樣子

所以我想要的是它不應該顯示錯誤是哪個字段,它應該直接顯示錯誤。

我的注冊表

<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']

我正在處理同樣的問題,但我找到了解決方案。

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

首先,您必須使用 for 循環來訪問每個字段及其錯誤消息。 然后只插入變量錯誤並應用條帶標簽(它會刪除項目符號)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM