Search code examples
pythonhtmlflaskregistration

How can I access html page through flask app and then another html page from the same flask app


I'm working on a project, developing it using flask . I want to link a html page with flask app and whenever the user click on register button of html page , I want that it redirect user to another html page through flask app. How can I do that ?


Solution

  • I hope I understood your question. You'd like to have a route that takes you to an HTML page and then be able to click a link on that page that will take you to another route.

    Here is how you can do that: The first function in app.py

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

    Your index.html file may look like this:

    <body>
    <a href="/register">Register</a>
    </body>
    

    The second function in app.py:

    @app.route('/register')
    def register():
       # Ensure the user reached path via GET
       if request.method == "GET":
          return render_template("another_file.html")
    
       else:
          pass # Pass is a Python way to say 'do nothing'
    

    I included a conditional statement in the second function because maybe you'd like the user to register by submitting a form. If you do this, you may want to do different things depending on the request method.