vendredi 14 novembre 2014

Django-bootstrap3 comment obtenir requis et classes error en modèle - Stack Overflow


In my registration form I have:


    class RegistrationForm(forms.Form):
required_css_class = 'required'
error_css_class='error'


username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput,
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput,
label=_("Password (again)"))

first_name=forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("first name"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
last_name=forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("last name"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})

.................

In my template that uses django-bootstrap3:


{% extends "registration/base.html" %}
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}

{# Display django.contrib.messages as Bootstrap alerts }
{% bootstrap_messages %}


{% block title %}Registration{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<h1>Sign up</h1>
<p>Already registered?
<a href="{% url 'django.contrib.auth.views.login' %}">Sign in here.</a></p>
</div>
</div>

<form action="{% url 'registration_register' %}"
method="post" role="form" class="form-horizontal">
{% csrf_token %}
<div class="form-group has-error">
{% bootstrap_form form layout='horizontal' %}

{% buttons %}
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Sign Me Up!
</button>
</div>
</div>
{% endbuttons %}
</form>

</div>
{% endblock %}

I am attaching a snapshot of the registration form: Three things I want to fix: 1. There is not * for the required field. It is missing although I have required_css_class = 'required' in the form



  1. All the fields are initially boxed in red (indication of error). the box should light up in red only when there is some error in form fields.


  2. possible way to override the label of form fields in template from what is assigned in the form using label attribute



enter image description here



In my registration form I have:


    class RegistrationForm(forms.Form):
required_css_class = 'required'
error_css_class='error'


username = forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("Username"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput,
label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput,
label=_("Password (again)"))

first_name=forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("first name"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
last_name=forms.RegexField(regex=r'^[\w.@+-]+$',
max_length=30,
label=_("last name"),
error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})

.................

In my template that uses django-bootstrap3:


{% extends "registration/base.html" %}
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript %}

{# Display django.contrib.messages as Bootstrap alerts }
{% bootstrap_messages %}


{% block title %}Registration{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<h1>Sign up</h1>
<p>Already registered?
<a href="{% url 'django.contrib.auth.views.login' %}">Sign in here.</a></p>
</div>
</div>

<form action="{% url 'registration_register' %}"
method="post" role="form" class="form-horizontal">
{% csrf_token %}
<div class="form-group has-error">
{% bootstrap_form form layout='horizontal' %}

{% buttons %}
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Sign Me Up!
</button>
</div>
</div>
{% endbuttons %}
</form>

</div>
{% endblock %}

I am attaching a snapshot of the registration form: Three things I want to fix: 1. There is not * for the required field. It is missing although I have required_css_class = 'required' in the form



  1. All the fields are initially boxed in red (indication of error). the box should light up in red only when there is some error in form fields.


  2. possible way to override the label of form fields in template from what is assigned in the form using label attribute



enter image description here


0 commentaires:

Enregistrer un commentaire