簡體   English   中英

使用 2 個 forms 將數據從 flask 發送到 html

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

我有 flask 將數據發送到 html。 現在,它第一次這樣做是通過填充主表的render_template('page1.html', data=data)來實現的。 現在,當我單擊主表的任何行時,我想通過url_for(entrypoint)再次調用 flask 然后我將再次為第二個表執行render_template('page1.html', data=data2) 但是如何區分它們呢? 我的意思是 html 將如何知道哪些數據是為誰而來的? 請指教。 我是 javascript 和 html 的新手。 我打算將主表和輔助表保留在不同的 forms 下。 請告知這是否是好的決定。

在我的 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);
            }
         });

這是 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>

在我的 app.py 里面是入口點的 flask 代碼:

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

這是到目前為止填充 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)

我在我的 flask 代碼中沒有看到這個被觸發。 我的 ajax 代碼有問題?? 另外,如何從 flask 獲取第二個表的相同 html 文件的數據?

非常感謝,蘇迪普

更新:錯誤是由於 ajax function 語法。 在 ajax 中的數據中添加了額外的 ')'...哎呀,這很糟糕

將此添加到 JAvascript 代碼中:

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

這是允許驗證 AJac 的令牌

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM