簡體   English   中英

url 路由 python/flask/neo4j

[英]url routing python/flask/neo4j

文檔的 id 需要在 url 作為可選參數。

  • egits 當前 https://..../view-document
  • 我需要它是 https://..../view-document/______

id 是用戶在 neo4j 數據庫中單擊的任何文檔,必須放在 url 的末尾

要更改的代碼部分是 routes.py

@app.route('/view-document', methods=['GET', 'POST'])
def view_document():
   
    document_link = active_document_link
    document_filepath = os.path.join('../static/sample_documents', document_link)
    document = get_document_from_document_link(document_link)
    user_name = get_active_user_name()**

index.js

function openDocument(documentFormat, documentLink) {
    fetch("/open-document", {
        method: "POST",
        body: JSON.stringify({ documentFormat: documentFormat, documentLink: documentLink })
    }).then((_res) => {
        window.location.href = "/view-document";
    });
}

我很確定這些是我需要更改才能工作的部分,而我設法解決的是

路線.py

@app.route('/view-document/<name>', methods=['GET', 'POST'])
def view_document():
    name = document['name'] <!--to access the neo4j database-->
   
    document_link = active_document_link <!--i was told to get the document link from the url instead of from "active_document_link" -->
    document_filepath = os.path.join('../static/sample_documents', document_link)

    document = get_document_from_document_link(document_link)
    user_name = get_active_user_name()

index.js

function openDocument(documentFormat, documentLink) {
    fetch("/open-document", {
        method: "POST",
        body: JSON.stringify({ documentFormat: documentFormat, documentLink: documentLink })
    }).then((_res) => {
        window.location.href = "/view-document/<name>";
    });
}

我可以想到幾個選項:

您還可以為同一個 function 定義多個規則。 然而,它們必須是獨一無二的。 也可以指定默認值。 例如,這里是一個接受可選頁面的 URL 的定義:

@app.route('/users/', defaults={'page': 1})
@app.route('/users/page/<int:page>')
def show_users(page):
    pass
  • 使用查詢字符串檢查 id 是否已傳遞給 controller

@app.route('/view-document', methods=['GET', 'POST'])
def view_document():
    name = request.args.get("name")

    if name:
        # Execute code here

暫無
暫無

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

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