Flask_wtf自定义 field样式(Placeholder, Style)、wtf.html、匹配 Bootstrap4、jinja2 quick_form

Flask-wtf + Flask-bootstrap


简简单单用一条语句就能让 jinja2 渲染出 form:wtf.quick_form(form)


但如果要实现自定义的表单样式,如下图,怎么做呢?


Live Demo: http://tianya.heroku.com/wtf




自定义错误提示:



很简单,修改3个地方就行:


forms.py

class CommentForm(Form):
    name = StringField('', validators=[Length(0, 64)], render_kw={
 "placeholder": "Your name",
                                                                  "style": "background: url(/static/login-locked-icon.png) no-repeat 15px center;text-indent: 28px"})
    email = StringField('', description='* We\'ll never share your email with anyone else.', validators= \
        [DataRequired(), Length(4, 64), Email(message=u"邮件格式有误")], render_kw={
 "placeholder": "E-mail: yourname@example.com"})
    comment = TextAreaField('', description=u"请提出宝贵意见和建议", validators=[DataRequired()],
                            render_kw = {
 "placeholder": "Input your comments here"})
    submit = SubmitField(u'提交')

复制 c:\git\tianya\venv\Lib\site-packages\flask_bootstrap\templates\bootstrap\wtf.html 

to: c:\git\tianya\app\templates\_wtf4.html


_wtf4.html

修改成 Bootstrap4 的告警样式

  {%- if field.errors %}
    {%- for error in field.errors %}
     <small class="form-text text-warning">{
 {error}}</small>
     {%- endfor %}
  {%- elif field.description -%}
<small class="form-text text-muted">{
 {field.description|safe}}</small>
   {%- endif %}
{%- endif %}

about.html

{% extends "
  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值