django explain the basics of ~ forms


Let's talk today a brief introduction django powerful forms capabilities
binary
   1 ModelFrom ModelFrom only need to declare the specified fields and other options to inherit
   2 Froms (our main push this today)
     1 Forms need to declare the provisions of each field
     2 forms.py file BaseForm , Form1, DeclarativeFieldsMetaclass
     . 3 froms.py inherits the properties of the relevant fields, it can directly call the file attributes
three Froms action
    and form data to check the legitimacy of an HTML form element to automatically generate 
    the second data type conversion (character type data into corresponding Python type)
four contents of
    a field of the metadata definition Definition 2 3 method override
five sheet method
    form Form1 = ({})
   1 form.is_valid (): # form data validation is legitimate return True / False
      Note 1 point if the form is there are extra fields, as long as the corresponding fields to meet the requirements, they may still return True
   2 form.errors: # output is the wrong format contains content tagged -> Dictionary
    attention to points
    1 Rear: take a field error messages form.errors.get ( 'key') [index ] -> It is noted here, there may be a plurality of error messages
    2 Front: take a field error error.k ey.index with <span> Label
    3 the rear end of the front end returns usage error {return: form.errors}
    . 4 common error types required, invalid
 3 form.cleand_data: Get form data # -> Dictionary
    Note 1 cleaned_data a consistent point value in the type field defines the type of Field.
               Value 2 value = form.clean_data.get embodiment
               3 stores data model.object.create (** form.cleand_data)
 . 4 show the distal end, carrying label bound forms will be automatically generated label should verify 
   1 form.as_p: # the P-tag form rendered
   2 form.key # take generating a field label
five fields associated control
   1 will automatically create a default primary key, if not specified
   2 max / min_length control the length restriction, CharFiled must
   3 required needs default, which is not empty
   4 error_messages custom error eg: error_messages = { 'required / vaild': 'Please enter your name'} ( actually not used), the dictionary can be established independent variable
six yuan data
  defined class Meta class
  db_table = '' in the database the real table name
seven sample code
 1 models module
  class User(models.Model):
  user=models.CharField(max_length=5)
  password=models.CharField(max_length=15)
  email=models.CharField(max_length=12)
  class Meta:
  db_table='User'
2 views模块
 def register(request):
 if request.method=='POST':
   biaodan=UserFrom(request.POST)
   if biaodan.is_valid():
      User.objects.create(**biaodan.cleaned_data)
      return HttpResponse('wae')
   else:

       = the UserForm form ()
      return the render (Request, 'register.html', about locals ())
 the else:
  form the UserForm = ()
  return the render (Request, 'register.html', about locals ()) into a local variable about locals #
3 forms module
  class UserFrom (forms.Form):
  user forms.CharField = (= 15 MAX_LENGTH, label = 'username', error_messages = { "required" : " user name needs require"})
  password = forms.CharField (MAX_LENGTH = 10 , label = 'password', the widget = widgets.PasswordInput (attrs = { 'class': 'Control-form'}))
  In email = forms.CharField (= 10 MAX_LENGTH, label = 'mailbox'))
4 front-end code
   rendering . 1
  <form Action = "/ Register /" Method = "the POST">
  {%}% csrf_token
  <div>
  username: {{form.user}} <span > {{error.user }}</span>
 </div>
  <div>
 密码: {{ form.password }}<span>{{ error.password }}</span>
  </div>
  <div>
  邮箱: {{ form.email }}<span>{{ error.email }}</span>
  </div>
  <div>
  <input type="submit" value="提交" />
   </div>
  </form>
渲染方法2
 <form action="/register/" method="POST">
 {% csrf_token %}
 {% for filed in form %}
 {% endfor %}
渲染方法3
<form action="/register/" method="POST">
{% csrf_token %}
{% for filed in form %}
<div class="form-row">
<label>{{ filed.label }}</label>// display label field specifies {%} endfor%</ div>{{filed.errors}} // displays error messages
{{filed}} // display field



<INPUT type = "Submit">

</ form>
nine added
a two-layer authentication mechanism 2 Forms Form 1 distal
end 2 custom package weight constraints display attributes
widget = widgets.type (attrs = { ' class':' form-control '})

Guess you like

Origin www.cnblogs.com/danhuangpai/p/10984420.html