【问题标题】:How can I pass jQuery Raty score to Wtforms or Flask or Python如何将 jQuery Raty 分数传递给 Wtforms 或 Flask 或 Python
【发布时间】:2016-01-01 08:29:56
【问题描述】:

首先祝你新年快乐。

我尝试使用 wtforms 表单获取 jQuery Raty 分数(数字类型)。但是我找不到将分数发送到我的后端的方法。

Blow 是我的 html 代码的关键部分:

{% block content %}
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="../static/js/jquery.raty.js"></script>
<link href="../static/css/jquery.raty.css" media="all" rel="stylesheet" type="text/css" />

<div class="row">
<div class="col-md-6">

<form method="POST" action="/test">
<div id="star"></div>
    {{ form.hidden_tag() }}
    {{ form.rating.label }} 
	{{ form.rating(id ='result') }}
    <input type="submit" value="Go">

</form>

<script type="text/javascript">
        $('#star').raty({
            path:"../static/img",
			score: '#result',
			targetKeep      : true,  
        });
</script>
</div>

{% endblock %}

对于python部分,我使用这个:

class rating(Form):
rating = TextAreaField()

@app.route('/test',methods=['GET', 'POST'])
def test():
    form = rating()
    if request.method == 'POST' and form.validate():
    stars = rating.data
    print stars
return render_template('test.html',form=form)

**所以在我运行我的服务器并达到星号之后,我的 python 控制台只打印

<property object at 0x00000000032DE408> 

而不是分数。**

谁能帮帮我,非常感谢。

【问题讨论】:

    标签: jquery python flask wtforms


    【解决方案1】:

    它会创建隐藏的输入“分数”,您必须在后端捕获它。

    <input type="hidden" name="score" value="3">
    

    【讨论】:

      【解决方案2】:

      rating 是您的表单类。 form 是您的表单实例。

      stars = form.rating.data
      

      如果您使用的是 Flask-WTF,则可以使用 validate_on_submit 简写。

      if form.validate_on_submit()
          stars = form.rating.data 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 2010-12-12
        相关资源
        最近更新 更多