0

I have a form on my home page that is generated from a list of folders on disk. I can't figure out how to route to the next page and add a dynamic url. So that the url reads http://127.0.0.1:5000/projects/ (http://127.0.0.1:5000/projects/banana) (http://127.0.0.1:5000/projects/apple)

@app.route("/")
def home():
    return render_template("home.html", list_projects=list_projects)


@app.route('/', methods=['POST', 'GET'])
def project_select_form():
    project_select = request.form['project']
    return redirect(url_for("project_page"), project=project_select)


@app.route('/projects/<project>', methods=['POST', 'GET'])
def project_page(project):
    return render_template("project_selected.html", project=project)



if __name__ == "__main__":


html

<form method = "POST">
  <select id="project" name="project">
      {% for x in list_projects %}
      <option value="{{ x }}">{{ x }}</option>
      {% endfor %}

  </select>
    <input name="text" type="submit">

</form>
3
  • use links <a href="/projects/{{ x }}">{{ x }}</a> instead of form and options. OR you will have to use JavaScript to change url in <form action="url"> Commented Jan 2, 2020 at 3:47
  • you can also create @app.route('/projects') and send form to this url <form action="/project"> and get name from request.form.get("project") Commented Jan 2, 2020 at 3:53
  • sorry it needs to be a form because there will be 100+ options, wouldnt want a page of links. all I want really is for the @app.route('projects/<project>') to have the string variable from the form from /. Commented Jan 2, 2020 at 4:58

2 Answers 2

2

All you need to do is, add a JavaScript/jQuery function that will call the URL.

<form method="GET">
    <select id="project" name="project">
        <option value="a1">a1</option>
        <option value="b1">b1</option>
    </select>
    <input type="button" value="Submit" onclick="myFunction()">
</form>

With this slight modification in your form, you can now have a function like this:

<script>
    function myFunction() {
        var e = document.getElementById("project");
        var list_project = e.options[e.selectedIndex].value;
        var theUrl = "http://127.0.0.1:5000/project/".concat(list_project);
        window.location.replace(theUrl);
    }
</script>

This simply calls the URL, which in turn renders the project_selected.html.

Hope this helps. Good luck.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the option. I am really trying to only use python and flask for this.
This is python and flask. You also need to have HTML pages for client-side.
so the aim is to get the url as example 127.0.0.1:5000/projects/apple I need the variable apple as well as it will be used to query the db for files. Bit new to this, hope it makes sense
the variable you need is here: {% for x in list_projects %}
0

The answer was to add an if statement to the form. Otherwise it errors out

@app.route("/", methods=['POST', 'GET'])
def home():

    if request.form:
        project_select = request.form['project']
        return redirect(url_for("project_page", project=project_select))
    else:
        return render_template("home.html", list_projects=list_projects)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.