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

发布时间:2020-06-16 21:21:53 作者:cosmos2031
来源:网络 阅读:258

下面的内容段是关于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)
推荐阅读:
  1. python简单的计算过期时间的源码
  2. 简单的jquery代码实现表单验证

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

表单 验证 dj

上一篇:面向对象_doc_module_class_dict_str_get_set_delitem

下一篇:Django----做一个简单网页的教程(适合初学者)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》