admin管理员组文章数量:1394526
I am creating blog posts and have so far done it with a normal html form. One funky think I was doing was running javascript onclick and setting a hidden variable in the form with extra data from the page. This was passed to the server just fine and gotten with request.form. Now I want to use wtf.quick_form for the entering of blog posts. qtf.quick_form works great for simple things, but now I need to run some javascript on click and then set a form variable. At the server, I need to retrieve this form variable. Does anyone know how to do this?
As an example, I tried this :
{{ wtf.form_field(form.submit, button_map={'submit':'new_entry_submit_button'}) }}
{{wtf.quick_form(form)}}
And then used jquery like this to intercept the submit button :
$(function(){
$('#new_entry_submit_button').bind('click', function(e) {
x = getSavedAndClickedAsString();
document.getElementsByName("srcLibArticles").item(0).value = x; <!--libArStr; -->
return true
});
});
None of this is working not to mention, I can't set a "hidden" field in the form. I don't know how to set a field in the form from the page. That's all handled under the hood.
Edit:
I found a Hidden field type for my form so I am including what my flask form looks like at the server :
class NewEntry(Form):
'''
A form for new entries
'''
title = TextAreaField("Title", validators=[Required()])
text = PageDownField("Text", validators=[Required()])
tags = TextAreaField("Tags", validators=[Required()])
srcLibEntries = HiddenField("srcLibEntries")
submit = SubmitField('Submit')
I am trying to write javascript that updates the hidden field upon submit and sends the updated hidden field back to the server.
Edit2: I have written the following html and it almost works but there are still some bizzare things happening :
<form method="post" role="form">
{{ wtf.form_field(form.title) }}
{{ wtf.form_field(form.text) }}
{{ wtf.form_field(form.tags) }}
{{ wtf.form_field(form.srcLibEntries, id="srcLibArticles") }}
{{ wtf.form_field(form.submit, button_map={'id':'new_entry_submit_button'}) }}
</form>
Most noteably, the id of the submit button will not change. Also, it is creating a label for the hidden input (set to the form variable name) and printing the label. The hidden input is there, but so is an annoying label which adds text to the page.
Edit 3:
I found out you can set the id of a form field right in python at your forms.py like this. (Ultimately, this is how I worked my example) :
class NewEntry(Form):
'''
A form for new entries
'''
title = TextAreaField("Title", validators=[Required()])
text = PageDownField("Text", validators=[Required()])
tags = TextAreaField("Tags", validators=[Required()])
srcLibEntries = HiddenField(label=None, id="srcLibArticles")
submit = SubmitField('Submit', id="new_entry_submit_button")
I am creating blog posts and have so far done it with a normal html form. One funky think I was doing was running javascript onclick and setting a hidden variable in the form with extra data from the page. This was passed to the server just fine and gotten with request.form. Now I want to use wtf.quick_form for the entering of blog posts. qtf.quick_form works great for simple things, but now I need to run some javascript on click and then set a form variable. At the server, I need to retrieve this form variable. Does anyone know how to do this?
As an example, I tried this :
{{ wtf.form_field(form.submit, button_map={'submit':'new_entry_submit_button'}) }}
{{wtf.quick_form(form)}}
And then used jquery like this to intercept the submit button :
$(function(){
$('#new_entry_submit_button').bind('click', function(e) {
x = getSavedAndClickedAsString();
document.getElementsByName("srcLibArticles").item(0).value = x; <!--libArStr; -->
return true
});
});
None of this is working not to mention, I can't set a "hidden" field in the form. I don't know how to set a field in the form from the page. That's all handled under the hood.
Edit:
I found a Hidden field type for my form so I am including what my flask form looks like at the server :
class NewEntry(Form):
'''
A form for new entries
'''
title = TextAreaField("Title", validators=[Required()])
text = PageDownField("Text", validators=[Required()])
tags = TextAreaField("Tags", validators=[Required()])
srcLibEntries = HiddenField("srcLibEntries")
submit = SubmitField('Submit')
I am trying to write javascript that updates the hidden field upon submit and sends the updated hidden field back to the server.
Edit2: I have written the following html and it almost works but there are still some bizzare things happening :
<form method="post" role="form">
{{ wtf.form_field(form.title) }}
{{ wtf.form_field(form.text) }}
{{ wtf.form_field(form.tags) }}
{{ wtf.form_field(form.srcLibEntries, id="srcLibArticles") }}
{{ wtf.form_field(form.submit, button_map={'id':'new_entry_submit_button'}) }}
</form>
Most noteably, the id of the submit button will not change. Also, it is creating a label for the hidden input (set to the form variable name) and printing the label. The hidden input is there, but so is an annoying label which adds text to the page.
Edit 3:
I found out you can set the id of a form field right in python at your forms.py like this. (Ultimately, this is how I worked my example) :
class NewEntry(Form):
'''
A form for new entries
'''
title = TextAreaField("Title", validators=[Required()])
text = PageDownField("Text", validators=[Required()])
tags = TextAreaField("Tags", validators=[Required()])
srcLibEntries = HiddenField(label=None, id="srcLibArticles")
submit = SubmitField('Submit', id="new_entry_submit_button")
Share
Improve this question
edited Mar 1, 2016 at 20:11
user442920
asked Feb 29, 2016 at 18:50
user442920user442920
8594 gold badges22 silver badges54 bronze badges
0
1 Answer
Reset to default 7Here is a very simple example of how to create a form with Flask-Bootstrap using WTForms (as it appears you are doing this in your code):
<form class="form form-horizontal" method="post" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.field1) }}
{{ wtf.form_field(form.field2) }}
</form>
The above is manually. Here is without thinking:
{{ wtf.quick_form(form) }}
To answer your question, well that is hard to do because you haven't shown us any errors. But one thing is that
$("#new_entry_submit_button")
is a jQuery selector for an id
attribute. To set that in Flask-Bootstrap either use:
{{ wtf.quick_form(form, id="whatever") }}
Or:
<form class="form form-horizontal" method="post" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field( form.field1(id='whatever') ) }}
{{ wtf.form_field(form.field2) }}
</form>
本文标签: flask wtfquickform running some javascript and setting a form variableStack Overflow
版权声明:本文标题:flask wtf.quick_form running some javascript and setting a form variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744100751a2590862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论