简体   繁体   English

使用 2 个 forms 将数据从 flask 发送到 html

[英]send data from flask to html with 2 forms

I have flask sending data to html.我有 flask 将数据发送到 html。 Now, first time it does that its by render_template('page1.html', data=data) which populates the main table.现在,它第一次这样做是通过填充主表的render_template('page1.html', data=data)来实现的。 Now when i click on any row of main table, i want to call flask again by url_for(entrypoint) and then again i will do render_template('page1.html', data=data2) for the 2nd table.现在,当我单击主表的任何行时,我想通过url_for(entrypoint)再次调用 flask 然后我将再次为第二个表执行render_template('page1.html', data=data2) But how to differentiate between them?但是如何区分它们呢? i mean how will html know which data is coming for whom?我的意思是 html 将如何知道哪些数据是为谁而来的? please advice.请指教。 I am novice in javascript and html.我是 javascript 和 html 的新手。 I am planning to keep the main table and secondary table under different forms.我打算将主表和辅助表保留在不同的 forms 下。 please advice if thats good decision or not.请告知这是否是好的决定。

Inside my html(page1.html), I have written在我的 html(page1.html) 里面,我写了

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
    function getId(element) {
    var row_index=element.rowIndex;

    $.ajax({
            url: '/get_details',
            data: document.getElementById("table1").rows[row_index].cells[5].innerHTML),
            type: 'POST',
            success: function(response){
                console.log(response);
            },
            error: function(error){
                console.log(error);
            }
         });

This is the code in html for table1 and table2(table2 not done yet)这是 html 中表 1 和表 2 的代码(表 2 尚未完成)

<section id="boxes" style="margin-top:-5%; margin-bottom:0%; position:absolute; z-index:1;">
        <div class="box" style="margin-left:30px; margin-top:20px; z-index:1;">
            <table id="table1">
                <tr>
                    <th>NO</th>
                    <th> SUBJECT NAME</th>
                    <th>ASSIGNED TO</th>
                    <th>CREATED</th>
                    <th>DISEASES</th>
                    <th>SUBJECT ID</th>
                    <th>STATUS</th>
                </tr>

                {% for row in data %}
                <tr onclick="getId(this)">
                    <td> {{ row[0] }}</td>
                    <td> {{ row[1] }}</td>
                    <td> {{ row[2] }}</td>
                    <td> {{ row[3] }}</td>
                    <td> {{ row[4] }}</td>
                    <td> {{ row[5] }}</td>
                    <td> {{ row[6] }}</td>
                  </tr>
                {% endfor %}
            </table>
        </div>
        <div class="box-two">
        </div>

Inside my app.py here is the flask code for the entry point:在我的 app.py 里面是入口点的 flask 代码:

@app.route('/get_details', methods=['POST'])
def get_details_user(patientid):
    print(patientid)

This is the code for the entrypoint for the records which populates table1 as of now:这是到目前为止填充 table1 的记录的入口点的代码:

@app.route('/records')
@login_required
def records():
    if current_user.priviledge:
        data = get_records_by_userid(None)
    else:
        data = get_records_by_userid(current_user.id)
    list_data = []
    for row in data:
        list_data.append([])
        for col, val in row.items():
            list_data[-1].append(val)
    return render_template('records.html', data=list_data)

I don't see this in my flask code being triggered.我在我的 flask 代码中没有看到这个被触发。 Something wrong in my ajax code??我的 ajax 代码有问题?? Also, how do I get the data from flask to this same html file for the second table?另外,如何从 flask 获取第二个表的相同 html 文件的数据?

Thanks a lot, Sudip非常感谢,苏迪普

Update: The error was coming due to ajax function syntax.更新:错误是由于 ajax function 语法。 Went with extra ')' in data in ajax...oops, thats bad在 ajax 中的数据中添加了额外的 ')'...哎呀,这很糟糕

Add this to the JAvascript code:将此添加到 JAvascript 代码中:

$.ajax(function() {
    headers = {'X-CSRFToken' : $('#csrf_token').val() },
    ...
});

This is the token the allows AJac to be validated这是允许验证 AJac 的令牌

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

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