简体   繁体   English

如何将用户输入从前端(即 HTML 表单)附加到 url

[英]How to append user input from front end (i.e. HTML form) to the url

I have a basic HTML form which includes a text box and a submit button.我有一个基本的 HTML 表单,其中包括一个文本框和一个提交按钮。 Let us say, the user types an input to the input box.假设用户在输入框中输入了一个输入。 I want to append that input to the existing URL of the home page.我想将该输入附加到主页的现有 URL。

Example: The URL of the home page be "localhost/home" and the user inputs "data" into the text box.示例:首页的 URL 为“localhost/home”,用户在文本框中输入“data”。

On the click of submit button, the URL should append the input and look something like: "localhost/home/data" (upon displaying the result fetched from backend using flask).单击提交按钮时,URL 应附加输入并类似于:“localhost/home/data”(显示使用 Flask 从后端获取的结果时)。

So, technically when I click submit, I want to be redirected to "localhost/home/data".因此,从技术上讲,当我单击提交时,我希望被重定向到“localhost/home/data”。

I'm using the below HTML form.我正在使用下面的 HTML 表单。

    <form action="/home" method="POST">
        <input type=text name="name">
        <input type=submit name="submit">
    </form>

and using this as the flask code snippet:并将其用作烧瓶代码片段:

    @app.route('/home', methods=['POST', 'GET'])
    def basic():
        if request.method == 'POST':
            path = (my path to json file)
            with open(path) as f:
                data = json.load(f)
            format_list=data[name]
            return format_list

        return render_template('temp.html')

How can I get the inputted parameters from the form appended to the URL?如何从附加到 URL 的表单中获取输入的参数? Thanks!谢谢!

When you use form submission with flask, the form is exposed on the flask object as a flask.request.form python dictionary.当您将表单提交与flask 一起使用时,表单在flask 对象上公开为flask.request.form python 字典。 From here, you can access what was POSTed to your endpoint:从这里,您可以访问发布到您的端点的内容:

@app.route('/home', methods=['POST', 'GET'])
def basic():
    if request.method == 'POST':
        path = request.form['name']

Do note you should be giving your input fields id attributes:请注意,您应该提供输入字段id属性:

<input id="name" name="name">

If you want to append this to a path or something of that nature, then you can further look into flask.redirect .如果您想将此附加到路径或类似性质的内容,那么您可以进一步查看flask.redirect

暂无
暂无

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

相关问题 如何将 html 模板中表单的输入附加到 url? - How do I append an input from a form in a html template to a url? 如何获取用户输入来创建另一个类的实例? (即从用户规范中添加员工?) - How to take user input to create another instance of a class? (i.e. add an employee from user specifications?) 如何从烧瓶询问用户输入(我正在使用反应为前端)? - How to ask user input from flask (I'm using react for front end)? 如何使用Flask根据用户输入对帖子列表(即新查询)进行重新排序 - How to reorder list of posts (i.e., new query) based on user input with Flask Python:如何获取索引的负索引(str,beg = 0,end = len(string)),即从结束计数 - Python: how can I get negative indices out for index(str, beg=0, end=len(string)), i.e. counting from end 使用BeautifulSoup从HTML中提取外国字符(即中文)? - foreign characters (i.e. Chinese) from HTML using BeautifulSoup? 如何根据 HTML 中的用户表单输入路由 Flask url? - How to route Flask url based on user form input in HTML? 如何从输出(即[])和其他删除括号 - How to remove brackets from output (i.e. [ ]) and other 有没有办法分隔用户输入的元素? (即-以mm / dd / yyy输入日期) - Is there a way to separate elements of user input? (i.e. - entering a date as mm/dd/yyy) Python-如何以结束线程的方式结束函数(即将threading.activeCount()减少1)? - Python - How to end function in a way that ends thread (i.e. decrease threading.activeCount() by 1)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM