Django component forms

Source: Internet
Author: User
Forms: Field Verification

For a single instance: Description of the registered user.

Model: models. py

class UserInfo(models.Model):    name=models.CharField(max_length=32)    pwd=models.CharField(max_length=32)    email=models.EmailField()    tel=models.CharField(max_length=32)

Template: register.html:

<! Doctype HTML> <HTML lang = "en"> 

View function: Register

# Forms component from Django. forms import widgetswid_01 = widgets. textinput (attrs = {"class": "form-control"}) wid_02 = widgets. passwordinput (attrs = {"class": "form-control"}) Class userform (forms. form): name = forms. charfield (max_length = 32, widget = wid_01) Pwd = forms. charfield (max_length = 32, widget = wid_02) r_pwd = forms. charfield (max_length = 32, widget = wid_02) email = forms. emailfield (widget = wid_01) Tel = forms. charfield (max_length = 32, widget = wid_01) def register (request): If request. method = "Post": form = userform (request. post) If form. is_valid (): Print (form. cleaned_data) # All clean fields and their corresponding values else: Print (form. cleaned_data) # print (form. errors) # errordict: {"fields with Incorrect verification": ["error message",]} print (form. errors. get ("name") # errorlist ["error message",] Return httpresponse ("OK") form = userform () return render (request, "register.html ", locals ())
Rendering tag function Rendering Method 1
<! Doctype HTML> <HTML lang = "en"> Rendering Method 2
<form action="" method="post">                    {% csrf_token %}                                        {% for field in form %}                        <div>                            <label for="">{{ field.label }}</label>                            {{ field }}                        </div>                    {% endfor %}                    <input type="submit" class="btn btn-default pull-right">                </form>
Rendering Method 3
<form action="" method="post">    {% csrf_token %}        {{ form.as_p }}    <input type="submit" class="btn btn-default pull-right"></form>
Display Error and reset input information function view
Def register (request): If request. method = "Post": form = userform (request. post) If form. is_valid (): Print (form. cleaned_data) # All clean fields and their corresponding values else: Print (form. cleaned_data) # print (form. errors) # errordict: {"fields with Incorrect verification": ["error message",]} print (form. errors. get ("name") # errorlist ["error message",] Return render (request, "register.html", locals () form = userform () return render (request, "register.html", locals ())
Template
<form action="" method="post" novalidate>    {% csrf_token %}        {% for field in form %}        <div>            <label for="">{{ field.label }}</label>            {{ field }} <span class="pull-right" style="color: red">{{ field.errors.0 }}</span>        </div>    {% endfor %}    <input type="submit" class="btn btn-default"></form>
Local hooks and global hooks
# Forms component from Django. forms import widgetswid_01 = widgets. textinput (attrs = {"class": "form-control"}) wid_02 = widgets. passwordinput (attrs = {"class": "form-control"}) from Django. core. exceptions import validationerrorclass userform (forms. form): name = forms. charfield (max_length = 32, widget = wid_01) Pwd = forms. charfield (max_length = 32, widget = wid_02) r_pwd = forms. charfield (max_length = 32, widget = wid_02) email = forms. em Ailfield (widget = wid_01) Tel = forms. charfield (max_length = 32, widget = wid_01) # partial hook def clean_name (Self): val = self. cleaned_data.get ("name") if not Val. isdigit (): Return Val else: Raise validationerror ("the user name cannot be a pure number! ") # Global hook def clean (Self): Pwd = self. cleaned_data.get ("PWD") r_pwd = self. cleaned_data.get ("r_pwd") if Pwd = r_pwd: return self. cleaned_data else: Raise validationerror ('the two passwords are inconsistent! ') Def register (request): If request. method = "Post": form = userform (request. post) If form. is_valid (): Print (form. cleaned_data) # All clean fields and their corresponding values else: clean_error = form. errors. get ("_ all __")
Return render (request, "register.html", locals () form = userform () return render (request, "register.html", locals ())
View
 <form action="" method="post" novalidate>            {% csrf_token %}            {% for field in form %}                <div>                    <label for="">{{ field.label }}</label>                    {{ field }}                    <span class="pull-right" style="color: red">                          {% if field.label == ‘R pwd‘ %}                          <span>{{ clean_error.0 }}</span>                          {% endif %}                          {{ field.errors.0 }}                    </span>                </div>            {% endfor %}            <input type="submit" class="btn btn-default"></form>

Django component forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.