最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to get data from HTML page to the server side using a hidden field through a fast render form? - Sta

programmeradmin0浏览0评论

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
  • What is render_form()? I can't find that in flask-wtforms's documentation. – AKX Commented Feb 4 at 8:34
  • Sorry I fot to mention that I use Bootstrap-Flask which allows fast rendering – my name Commented Feb 4 at 8:54
  • Right. Well, you're now essentially having multiple renders of a single task-form on the page (with the same id too, which is a no-no in HTML) – one approach would be to pass a get_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:59
  • 1 default="{{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
Add a comment  | 

1 Answer 1

Reset to default 0

Instead 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).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论