• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python+django实现的简单的表单验证代码

python 水墨上仙 2834次浏览

python+django实现的简单的表单验证代码,带有html页面,表单类,和view类的全部代码。

HTML代码


<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">
    <h2>Form validation example</h2>
    {% 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
    # http://www.75271.com
    form = ContactForm(request.POST)
  else:
    form = ContactForm()
  
  data = {
    'form': form,
  }
  return render(request, 'contact_form.html', data)


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python+django实现的简单的表单验证代码
喜欢 (0)
加载中……