How to hyperlink to html

Viewed 75

Please help, I have provided below three files: application.py, ask.html, and link.html. In ask.html, I'm trying to ask the user to provide me a link. They will then be redirected to link.html where there is a button to take them to that link. But once the button is pressed, it doesn't take me to the link, comes out error 404 not found.

In application.py:

@app.route("/")
def ask():
    return render_template("ask.html")

@app.route("/link")
def link():
    weblink = request.args.get("weblink")
    return render_template("link.html", weblink=weblink)

In ask.html:

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Ask</title>
    </head>
    <body>
        <form action="/link">
            <input placeholder="weblink here" name="weblink" type="text">
            <input type="submit">
        </form>
    </body>
</html>

In link.html:

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Ask</title>
    </head>
    <body>
        <a href="{{ weblink }}" class="btn btn-primary">Go to Link</a>
    </body>
</html>
2 Answers

First, change:

<form action="/link">

to:

<form action="/link" method="post">

Then, in link():

@app.route("/link", methods = [“GET”, “POST”])
def link():
    weblink = request.form.get("weblink")
    ...

Change

  <form action="/link">

To

<form action = "{{url_for('link')}}" method = "post">

in application.py change

@app.route('/link')

to

@app.route('/link', methods=['GET', 'POST'])
def link():
    if request.method == 'POST':
          weblink = request.form.get('weblink')
          return render_template('link.html', weblink=weblink)
Related