中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python+django實現的簡單的表單驗證源碼

發布時間:2020-06-16 21:21:53 來源:網絡 閱讀:258 作者:cosmos2031 欄目:編程語言

下面的內容段是關于python+django實現的簡單的表單驗證的內容,應該能對大伙也有用途。

<html>
<head>
  <title>Form validation example</title>
  <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/bootstrap.css">
</head>
<body>

  <div class="col-sm-8">
    <h3>Form validation example</h3>
    {% if form.errors %}
    <div class="text-danger">Please correct the error{{ form.errors|pluralize }} below.</div>
    {% endif %}
    <p>
    <div class="form-horizontal">
      <form action="" method="post" role="form">{% csrf_token %}

        <div class="form-group row {% if form.subject.errors %}has-error{% endif %}">
           <label for="id_subject" class="col-sm-3 control-label">Email subject:</label>
           <div class="col-lg-5">
            {{ form.subject }}
            {% if form.subject.errors %}
              <span class="help-block">
                {% for error in form.subject.errors %}{{ error }}{% endfor %}
              </span>
            {% endif %}
          </div>
        </div>

        <div class="form-group row {% if form.email.errors %}has-error{% endif %}">
          <label for="id_sender" class="col-sm-3 control-label">Email address:</label>
          <div class="col-lg-5">
          {{ form.email }}
            {% if form.email.errors %}
              <span class="help-block">
                {% for error in form.email.errors %}{{ error }}{% endfor %}
              </span>
            {% endif %}
          </div>
        </div>

        <div class="form-group row {% if form.message.errors %}has-error{% endif %}">
          <label for="id_message" class="col-sm-3 control-label">Message:</label>
          <div class="col-lg-5">
            {{ form.message }}
            {% if form.message.errors %}
              <span class="help-block">
                {% for error in form.message.errors %}{{ error }}{% endfor %}
              </span>
            {% endif %}
          </div>
        </div>

        <div class="form-group row">
          <div class="col-sm-5 col-sm-offset-3">
            <input type="submit" value="Validate form" class="btn btn-primary" />
          </div>
        </div>
      </form>
    </div>
  </div>
</body>
</html>

forms.py

from django import forms

class ContactForm(forms.Form):
  """
  define a contact form class
  """
  # this will be rendered like
  # <input class="form-control" id="id_subject" name="subject" size="48" type="text">
  # valid if not empty
  subject = forms.CharField(widget=forms.TextInput(attrs={'size':'48', 'class':'form-control'}))
  # A CharField that checks that the value is a valid email address.
  email = forms.EmailField(widget=forms.TextInput(attrs={'size':'48', 'class':'form-control'}))
  message = forms.CharField(widget=forms.Textarea(attrs={'cols':50, 'rows': 5 , 'class':'form-control'}))

views.py

from django.shortcuts import render
from .forms import ContactForm

def contact(request):
  if request.method == 'POST':
    # get data from POST request to contactform
    form = ContactForm(request.POST)
  else:
    form = ContactForm()

  data = {
    'form': form,
  }
  return render(request, 'contact_form.html', data)
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

敖汉旗| 文山县| 通许县| 石屏县| 合山市| 万年县| 南安市| 凉城县| 金昌市| 永平县| 凌云县| 文化| 湖南省| 乌兰县| 获嘉县| 凤庆县| 承德县| 巴彦淖尔市| 玉龙| 古丈县| 杂多县| 高陵县| 甘孜| 南投市| 东兰县| 亳州市| 法库县| 任丘市| 富民县| 乐业县| 梅河口市| 白山市| 呼伦贝尔市| 财经| 贺州市| 交口县| 普兰县| 宁武县| 抚州市| 彭山县| 安泽县|