【问题标题】:url routing python/flask/neo4jurl 路由 python/flask/neo4j
【发布时间】:2022-09-27 20:42:23
【问题描述】:

文档的 id 需要作为可选参数在 url 中。

  • 例如它当前的 https://..../view-document
  • 我需要它是 https://..../view-document/______

id 是用户在 neo4j 数据库中单击的任何文档,必须放在 url 的末尾

要更改的代码部分是 路线.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\";
    });
}

我很确定这些是我需要更改才能工作的部分 iv设法解决的是

路线.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>\";
    });
}

    标签: python flask url-routing dynamic-url


    【解决方案1】:

    我可以想到几个选项:

    您还可以为同一个函数定义多个规则。然而,它们必须是独一无二的。也可以指定默认值。例如,下面是接受可选页面的 URL 的定义:

    @app.route('/users/', defaults={'page': 1})
    @app.route('/users/page/<int:page>')
    def show_users(page):
        pass
    
    • 使用查询字符串检查 id 是否已传递给控制器

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

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 2018-09-25
      • 1970-01-01
      • 2018-09-26
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多