【问题标题】:get input form not as variable but as url获取输入表单不是作为变量而是作为 url
【发布时间】:2016-05-06 12:21:03
【问题描述】:

我正在构建一个网络服务,以便人们可以搜索数据库。可以说我有用户和公司。每个用户和公司都可以通过他们的 id 找到。因此,如果您搜索myurl/users/<id>,您将获得该用户的信息,另一方面,如果您搜索公司/您将获得该公司的信息。 为此,我创建了两个简单的输入文本(一个用于用户,另一个用于公司),人们可以在其中键入<id>。我的问题是,当我从输入文本中获取值时,我得到的是 myrul/users?<id> 而不是 myurl/users/id。我尝试对斜线进行硬编码,但后来得到myrul/users/?<id>。 所以我的问题是如何将输入文本作为 url 而不是变量。

我正在使用烧瓶,所以我的 html 有这样的 jinja2 代码:

<!-- USER id -->
    <form method='GET' action={{url_for('get_info_by_id', type_collection='user')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                         
    </form>

<!-- COMPANY id-->
    <form method='GET' action={{url_for('get_info_by_id', type_collection='company')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                      
    </form>

在我的 python 脚本(烧瓶)中

@app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
    # search into the database and return info about that id

【问题讨论】:

  • 如果您希望将表单变量放置在 URL 中而不是查询字符串或帖子正文中,则需要使用 JavaScript 来捕获表单提交事件并导航到新 URL。跨度>
  • @dirn 是正确的,但是如果您只是想让表单搜索数据库并返回您的对象,请查看我的答案。
  • 我会通过JavaScript,谢谢。

标签: html url flask get


【解决方案1】:

正如@dirn 在评论中所建议的,我是通过 JavaScript 实现的,如果其他人也有兴趣,这里是代码:

HTML:

<!-- USER id -->
<form method='GET' class="search" id="user" action="">
    <input type="text" name="my_id"/><input type="submit" value="Go">
</form>

<!-- COMPANY id-->
<form method='GET' class="search" id="company" action="">
    <input type="text" name="my_id"/><input type="submit" value="Go">
</form>

JS:

$(".search").submit(function( event ){
    event.preventDefault();
    var my_id = $(this).find(":input").val();
    url = 'myurl/'+ $(this).attr("id") + '/' + my_id;
    window.location.href = url;     
});

蟒蛇(烧瓶)

@app.route('myurl/<type_collection>/<my_id>')
get_info_by_id(type_collection,my_id):
    # search into the database and return info about that id

【讨论】:

    【解决方案2】:

    您是否有不能使用该变量的原因,或者您只是想将其放入 URL 以便进行搜索?我冒昧地认为您只是希望表单和数据库搜索能够正常工作,因此请尝试以下操作。

    像这样调整你的路线:

    @app.route('myurl/<type_collection>/')
    def findAllTheThings():
        if not request.form['my_id']: # Just check if a specific entity is chosen
            return render_template('YourTemplateHere') # If no entity, then render form 
        entity_id = request.form['my_id']
        get_info_by_id(type_collection, entity_id):
        # search into the database and return info about that id
    

    现在调整模板如下:

    <!-- USER id -->
    <form method='GET' action={{url_for('findAllTheThings', type_collection='user')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                         
    </form>
    
    <!-- COMPANY id-->
    <form method='GET' action={{url_for('findAllTheThings', type_collection='company')}}>
        <input type="text" name="my_id"/><input type="submit" value="Go">                      
    </form>
    

    现在,如果没有选择实体,您将只呈现表单。您可以闪一下,让他们知道他们需要选择一个特定的 ID,或者只是让他们弄清楚。如果实体已被选中,您将正确调用该函数。

    【讨论】:

    • 谢谢,但我确实需要将其设为 url 而不是变量
    猜你喜欢
    • 2017-07-06
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2023-03-10
    • 2014-11-14
    • 2016-01-26
    相关资源
    最近更新 更多