Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

bootstrap 4 - render Bootstrap4 Form "is-invalid" class from a view using django and crispy

i'm trying to do a server side verification of a django form, a simple form that contains user infos, the verification is done to the Email field in the views.py. As the title says, i want to render the same old form but with email input class =".... is-invalid" in case there is an entry in database with the same email.

the form should be something like that : Server side

below is the views.py:

def client_create_view(request):
form = ClientForm(request.POST or None)
if form.is_valid():
    obj = form.save(commit=False)
    mail = form.cleaned_data['Mail']
    user = Client.objects.get(Mail=mail)
    if user != None:
        print("user exists @: " + user.Mail)
        form.invalid()
        context = {
            'validation': False,
            'form':form
        }
        return render(request, "clients/test.html", context)

forms.py:

class ClientForm(forms.ModelForm):
FName       = forms.CharField(label='First Name', widget=forms.TextInput(attrs={"placeholder":"Your First Name", "class":"form-control", "id":"validationCustom01"}),required=True)
LName       = forms.CharField(label='Last Name', widget=forms.TextInput(attrs={"placeholder":"Your Last Name"}),required=True)
Phone       = forms.CharField(label='Phone Number', widget=forms.TextInput(attrs={"placeholder":"Your Phone Number"}),required=True)
Mail        = forms.EmailField(label='Email', widget=forms.TextInput(attrs={"placeholder":"Your Email"}),required=True)
Address     = forms.CharField(label='Address', widget=forms.TextInput(attrs={"placeholder":"Your Address"}),required=True)
Password    = forms.CharField(label='Password', widget=forms.TextInput(attrs={"placeholder":"Password", "type":"password"}),required=True)
class Meta:
    model = Client
    fields = [
        'FName',
        'LName',
        'Phone',
        'Mail',
        'Address',
        'Password'
    ]

test.html:

{% load crispy_forms_tags %}
{% block content %}

<form id="example-form" method="post" class="d-flex justify-content-center align-items-center row g-3 needs-validation" novalidate>
    {% csrf_token %}
    {% comment %} {{ form|crispy }} {% endcomment %}
    <div class="col-md-3">
        {{ form.FName|as_crispy_field }}
        {{ form.Mail|as_crispy_field }}
        <div class="valid-feedback">
            Looks good!
        </div>
        {% comment %} {{ form.Mail|as_crispy_field }} {% endcomment %}
    </div>
    <div class="d-flex justify-content-center align-items-center col-12">
        <button type="submit" class="btn btn-success">Enregistrer</button>
    </div>
</form>
{% include 'footer.html' %}
{% endblock %}
question from:https://stackoverflow.com/questions/65894442/render-bootstrap4-form-is-invalid-class-from-a-view-using-django-and-crispy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...