admin管理员组文章数量:1305133
Hi guys Here's the problem I'm using a fast render form using Bootstrap-Flask that contains a hidden field and I want to get back a record ID through it but of course,e I can't write simple Jija2 in it because I can't access it from the HTML page or that what I think here's what I have and some solutions I tried (note: I know that if used a different way of rendering the form will be easier to handle this but the core of the question is there a way to do that while using fast render**)**
this is the form
class TaskForm(FlaskForm):
title = StringField(label="Task Title", validators=[DataRequired()])
description = CKEditorField(label="Description", validators=[DataRequired()])
list_id = HiddenField(
label="List id",
validators=[DataRequired()],
default="{{list.id}}",
)
submit = SubmitField("Add task")
I have a container of todo list that I iterate through it
{% for list in all_lists %}
<li class="list col col-sm-3 justify-content-start">
<h5 class="card-title text-muted text-uppercase text-start">
{{ list.title }}
</h5>
<p style="display: none">{{ list.description }}</p>
<hr />
<ol class="fa-ol ol-task justify-content-start">
{% for task in list.ltasks %}
<!-- Cards to do-->
<li class="task justify-content-start">
<h6 class="card-title text-muted text-uppercase text-start" >
{{ task.title }}
</h6>
<hr />
<p class="card-text">{{ task.description }}</p>
</li>
{% endfor %}
<li class="task justify-content-start">
{{ ckeditor.load(pkg_type="basic") }}
{{ ckeditor.config(name='body') }}
{{ render_form(task_form ,novalidate = True, id = "task-form" ) }} <-- Fast rendered form -->
</li>
</ol>
</li>
{% endfor %}
So I have tried to pass the default attribute as seen above and also used render_kw={"value":"{{ list.id }}"} I even tried to the field using javascript and append it to the form which has worked but the list.id only shows up as a string like this:
<input id="list_id" name="list_id" required="" type="hidden" value="{{list.id}}">
and also tried to pass it with different styles like (list.id), and (List.id) with a capital L like the DB class itself, I even tried to pass (db.get_or_404(List,>id<) but remembered that I need the id :D
I am still learning and knowing the nooks and crannies is part of the quest Thanks in advance
Hi guys Here's the problem I'm using a fast render form using Bootstrap-Flask that contains a hidden field and I want to get back a record ID through it but of course,e I can't write simple Jija2 in it because I can't access it from the HTML page or that what I think here's what I have and some solutions I tried (note: I know that if used a different way of rendering the form will be easier to handle this but the core of the question is there a way to do that while using fast render**)**
this is the form
class TaskForm(FlaskForm):
title = StringField(label="Task Title", validators=[DataRequired()])
description = CKEditorField(label="Description", validators=[DataRequired()])
list_id = HiddenField(
label="List id",
validators=[DataRequired()],
default="{{list.id}}",
)
submit = SubmitField("Add task")
I have a container of todo list that I iterate through it
{% for list in all_lists %}
<li class="list col col-sm-3 justify-content-start">
<h5 class="card-title text-muted text-uppercase text-start">
{{ list.title }}
</h5>
<p style="display: none">{{ list.description }}</p>
<hr />
<ol class="fa-ol ol-task justify-content-start">
{% for task in list.ltasks %}
<!-- Cards to do-->
<li class="task justify-content-start">
<h6 class="card-title text-muted text-uppercase text-start" >
{{ task.title }}
</h6>
<hr />
<p class="card-text">{{ task.description }}</p>
</li>
{% endfor %}
<li class="task justify-content-start">
{{ ckeditor.load(pkg_type="basic") }}
{{ ckeditor.config(name='body') }}
{{ render_form(task_form ,novalidate = True, id = "task-form" ) }} <-- Fast rendered form -->
</li>
</ol>
</li>
{% endfor %}
So I have tried to pass the default attribute as seen above and also used render_kw={"value":"{{ list.id }}"} I even tried to the field using javascript and append it to the form which has worked but the list.id only shows up as a string like this:
<input id="list_id" name="list_id" required="" type="hidden" value="{{list.id}}">
and also tried to pass it with different styles like (list.id), and (List.id) with a capital L like the DB class itself, I even tried to pass (db.get_or_404(List,>id<) but remembered that I need the id :D
I am still learning and knowing the nooks and crannies is part of the quest Thanks in advance
Share Improve this question edited Feb 4 at 8:53 my name asked Feb 4 at 7:31 my namemy name 11 bronze badge 4 |1 Answer
Reset to default 0Instead of passing in a single task_form
(e.g. render(..., task_form=TaskForm())
to your template, you can pass in the actual form class TaskForm
, e.g. render(..., TaskForm=TaskForm)
.
and then, in your template,
{{ render_form(TaskForm(data={"list_id": list.id}), ...
to seed that instance of the task form with this particular list's ID.
This method has its caveats (namely, no POSTed data will ever get bound to this form, so handling and showing validation errors may be a bit tough), but shows the basic idea. You could also prepare a dict of these forms in your Python code, one per list id (preferably then with a prefix=...
set).
本文标签:
版权声明:本文标题:javascript - Is it possible to get data from HTML page to the server side using a hidden field through a fast render form? - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741782850a2397409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
render_form()
? I can't find that inflask-wtforms
's documentation. – AKX Commented Feb 4 at 8:34task-form
on the page (with the sameid
too, which is a no-no in HTML) – one approach would be to pass aget_task_form(list_id=...)
function down to the template, which would return a new task form with the correct value. – AKX Commented Feb 4 at 8:59default="{{list.id}}"
- apparently those type of placeholders don't work in the context that you tried to use them in, so you get the literal{{list.id}}
in the generated HTML for the field. You'll probably need to find a way to pass this to the form renderer from your template. – C3roe Commented Feb 4 at 9:01