简体   繁体   English

更新wtforms中的提交按钮文本

[英]Update text of submit button in wtforms

I have a form, that will be used for a new submit and updates. 我有一个表单,将用于新的提交和更新。 My question is about the text of the submit button. 我的问题是关于提交按钮的文本。 I want to change the text to New submit and to New update , depending on the situation. 我想根据具体情况将文本更改为新提交新更新 This is purely informative. 这纯粹是提供信息的。

class Interview(Form):
    ...
    submit = SubmitField('New submit')  

If possible, i want to avoid create a new class, with exactly same fields, only because of the text of submit. 如果可能的话,我想避免创建一个具有完全相同字段的新类,只是因为提交文本。

The correct way to do this with mixins: 使用mixins执行此操作的正确方法:

class InterviewMixin():
    ...

class InterviewSubmit(Form, InterviewMixin):
    submit = SubmitField('New submit')

class InterviewUpdate(Form, InterviewMixin):
    submit = SubmitField('New update')

Old question, but for anyone else coming across this, an alternative is to just set it from code before rendering the template: 老问题,但对于遇到此问题的其他人来说,另一种方法是在呈现模板之前从代码中设置它:

if is_submit:
    form.submit.label.text = 'New submit'
else:
    form.submit.label.text = 'New update'

return render_template(...)

I've solved the problem by not incorporating the submit button in the form definition, instead I add a submit button in the HTML, based on what label I want on it. 我已经通过不在表单定义中包含提交按钮来解决问题,而是根据我想要的标签在HTML中添加提交按钮。

{% if pagetitle == 'Update' %}
   <p><input type="submit" name="btn" value="New Update"></p>
{% endif %}
{% if pagetitle == 'Submit' %}
   <p><input type="submit" name="btn" value="New Submit"></p>
{% endif %}

Using this way you can use the same form, and have different labels on the button, depending on which one should be used. 使用这种方式,您可以使用相同的表单,并在按钮上具有不同的标签,具体取决于应使用哪个。

If you need you can use the same variable to set the action of the form, if you need to direct to different views. 如果需要,可以使用相同的变量来设置表单的操作,如果需要指向不同的视图。

Or you can use the value of the button in the view. 或者您可以使用视图中按钮的值。

if flask.request.form['btn'] == 'New Update':
   ...
elif flask.request.form['btn'] == 'New Submit':
   ...

The correct way would be to make two forms and check which submit button is pressed at server side. 正确的方法是制作两个表单并检查在服务器端按下了哪个提交按钮。

if submit_form.validate_on_submit() and submit_form.any_field.data:
        print "submit_form submit button is Pressed"
elif update_form.validate_on_submit() and update_form.any_field.data:
        print "update_form submit button is submitted"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM