在 python 形式中使用 uk-postcode-utils

Using uk-postcode-utils in a python form

我正在创建一个表单,供用户输入有效的英国邮政编码。我目前有以下表格代码:

class RegisterForm(Form):
    first_name = StringField('First Name', validators=[DataRequired(), validators.Length(min=1, max=50)])
    last_name = StringField('Last Name', validators=[DataRequired(), validators.Length(min=1, max=50)])
    centre_id = StringField('Centre ID', validators=[DataRequired(), validators.Length(min=1, max=11)])
    doctor_id = StringField('Doctor ID', validators=[DataRequired(), validators.Length(min=1, max=11)])
    address = StringField('Address Line 1', validators=[DataRequired(), validators.Length(min=1, max=100)])
    town_name = StringField('Town Name', validators=[DataRequired(), validators.Length(min=1, max=50)])
    county_name = SelectField('County Name', choices=[('antrim', 'Antrim'), ('armagh', 'Armagh'), ('down', 'Down'), ('derry/londonderry', 'Derry/Londonderry'), ('fermanagh', 'Fermanagh'), ('tyrone', 'Tyrone')], validators=[DataRequired()])
    postcode = StringField('Postcode', validators=[DataRequired(), validation.is_valid_postcode])
    telephone_number = TelField('Telephone Number', validators=[DataRequired(), validators.Length(min=11, max=11)])
    email_address = EmailField('Email Address', validators=[DataRequired()])
    patient_username = StringField('Username', {validators.DataRequired(), validators.EqualTo('confirm_patient_username', message='Usernames do not match')})
    confirm_patient_username = StringField('Confirm Username')
    patient_password = PasswordField('Password', {validators.DataRequired(),validators.EqualTo('confirm_patient_password', message='Passwords do not match')})
    confirm_patient_password = PasswordField('Confirm Password')

模板是:

{% extends 'patient/patient_login_layout.html' %}

{% block body %}
    <h1>Patient Register</h1>
    {% from 'includes/_formhelpers.html' import render_field %}
    <form method="POST" action="">
        <div class="form-group">
            {{render_field(form.first_name, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.last_name, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.centre_id, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.doctor_id, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.address, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.town_name, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.county_name, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.postcode, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.telephone_number, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.email_address, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.patient_username, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.confirm_patient_username, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.patient_password, class="form-control")}}
        </div>
        <div class="form-group">
            {{render_field(form.confirm_patient_password, class="form-control")}}
        </div>
        <p><input type="submit" class="btn btn-primary" value="Submit"></p>
    </form>
{% endblock %}

但出现错误:TypeError: argument of type 'StringField' is not iterable.

我该怎么做才能解决这个问题?

编辑: 这是完整的表单条目和网页模板。 非常感谢。

您没有在 postcode StringField 中正确使用 WTForms 验证器。

声明一个采用表单和字段参数的函数,并在 post 代码无效时引发错误,在此函数中您使用 uk-postcode-utils is_valid_postcode 函数,即:

from wtforms.validators import ValidationError
from ukpostcodeutils import validation

def post_code_validator(form, field):
    if not validation.is_valid_postcode(field.data):
        raise ValidationError('Invalid UK Post Code')

在 StringField 上注册验证器:

class RegisterForm(Form):
    #  ...
    postcode = StringField('Postcode', validators=[DataRequired(), post_code_validator])
    # ...

如果 post 代码字段以多种形式使用,您最好编写一个 class 验证器,如 WTForms documentation.

中所述

类似于(未测试):

from wtforms.validators import ValidationError
from ukpostcodeutils import validation

class PostCodeValidator(object):
    def __init__(self, message=None):
        if not message:
            message = u'Must be a valid UK Post Code'
        self.message = message

    def __call__(self, form, field):
    if not validation.is_valid_postcode(field.data):
        raise ValidationError('Invalid UK Post Code')

post_code_validator = PostCodeValidator

class RegisterForm(Form):
    #  ...
    postcode = StringField('Postcode', validators=[DataRequired(), post_code_validator])