首页 文章

Flask WTF表格的经度纬度

提问于
浏览
1

我试图让Flask WTF表格读取经度和纬度,但我找不到合适的表格字段 . 我找到了一些成功使用FloatField的例子,但是这个例子真的很旧,我认为WTF表格有变化 .

这是我正在使用的Form类:

class GetFieldForm(Form):
    field_name = StringField(Designation')
    field_latitude = FloatField(u'Latitude', default=-30, validators=[InputRequired()], description='48.182601')
    field_longitude = FloatField(u'Longitude', default=150,
                                 validators=[InputRequired()], description='11.304939')
    submit = SubmitField(u'Signup')

将值从表单传递到Google Map :

@frontend.route('/demo')
def demo():
        field_form = GetFieldForm()
        if field_form.validate_on_submit():
            flash('Hello, {}. You have successfully signed up'
                  .format(escape(field_form.name.data)))

            field_latitude = field_form.field_latitude.data
            field_longitude = field_form.field_longitude.data
            mymap = Map(
                identifier="view-side",
                lat=field_latitude,
                lng=field_longitude,
                zoom=18,
                style="height:720px;width:720px;margin:0;",  # hardcoded!
                markers=[(field_latitude, field_longitude)],
                maptype='SATELLITE'
            )

            return render_template('demo.html', mymap=mymap, field_form=field_form)

使用以下Jinja2模板:

<div class="container-fluid">

            <div class="row-fluid">

                <div id="map" class="col-md-7">
                    {{mymap.html}}
                </div>

               <div class="col-md-5">
                   <div class="panel panel-default">
                <!-- Panel Title -->
                <div class="panel-heading">
                    <h2 class="panel-title text-center">Locate Your Field  <span class="glyphicon glyphicon-map-marker"></span></h2>

                 <div class="panel-body">
                 {% if request.args.get('legacy') -%}
                   {{wtf.quick_form(field_form, novalidate=True)}}
                 {%- else -%}
                   {{field_form|render_form()}}
                 {%- endif %}
                     </div>
               </div>
            </div>
        </div>

问题是,表单不允许我将经度和纬度作为浮点数传递:
enter image description here

我正在使用的版本:

烧瓶0.12

Flask-WTF 0.14.2

Flask-GoogleMaps https://github.com/rochacbruno/Flask-GoogleMaps

编辑:提示我错误可能与FLASK-Bootstrap>有关

问题是你似乎正在使用的Flask-Bootstrap . https://github.com/mbr/flask-bootstrap/blob/master/flask_bootstrap/forms.py#L97-L99它覆盖浮点类型以使用“数字”作为输入类型,这在那里并不完全正确,因为“如果未提供步骤参数,则“在html5中的数字”仅限于整数 . 因此,您必须在输入字段上设置step属性,但我不知道如何使用Flask-Bootstrap执行此操作 . 优选地,诸如步骤=“0.000001”之类的东西就足够了 .

但是我如何通过步骤参数超出了我...

1 回答

  • 0

    找到了解决方案 . 而不是像这样生成表单:

    {{field_form|render_form()}}
    

    做这个:

    {{wtf.quick_form(field_form, novalidate=True)}}
    

相关问题