0

I have a login page as login.py which renders login.html. On submit i want to redirect to mainpage.py which renders mainpage.html. But on submit click it just doesn't redirect. I get a page not found error. And the url remains the same

@app.route('/login',methods = ['GET','POST'])
def index():
    try:
        if request.method =='POST':
            db_user = request.form['db_user']
            db_pwd = request.form['db_pwd']
            if (connect(db_user,db_pwd)==1):
                return redirect("/mainpage", code=302)
        else:
            return render_template('login.html')    
    except Exception as e:
        print(("error :", str(e)))
        return render_template('login.html')

What should be mentioned in the redirect option? is it the html filename or py filename where the html gets rendered? I tried with html filename,py filename and the name in the @app.route. but without success

4
  • 1
    Try adding the else block for db connect. Commented Apr 28, 2017 at 11:16
  • So is the redirect method correct? Should this be html file name or .py file name mentioned here? Commented Apr 28, 2017 at 12:12
  • @kten redirect(url_for("your_function_name")) is better, but specifying an URL, like /mainpage should be OK. Commented Apr 28, 2017 at 12:17
  • Should this link (/main) be defined somewhere else? other than the mainpage.py itself? Commented Apr 28, 2017 at 12:23

2 Answers 2

3

The cause of the 404 error:

When you redirect to /mainpage, Flask will be looking for a route handler in your login.py file, not mainpage.py. Since login.py doesn't have a route handler for /mainpage, it gives you a 404. It never reaches mainpage.py.

What you are trying to do and possible solutions:

What you are trying to do is separating your routes across different files. For that, you need to explicitly tell Flask where your route handlers are (in other words, in which files it should look for route handlers).

There are two ways for doing that:

  1. See this documentation (Larger Applications) to learn how to organize your files as a package. You'll have to import mainpage.py into login.py, or vice-versa.

  2. Or use a blueprint: Modular Applications with Blueprints.

Please note that you'll only need that if your application has a substantial size. If you have just a few route handlers, you should probably keep it simple with everything in one single file.

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

2 Comments

Thank you. this was exactly the problem. But one query. If i need to use separate files and need to import the file into the other file as mentioned. Does the file have to be defined as a class? Or is it possible to import after it is defined as packages?
I believe both package and module should work, but Flask documentation suggests using a package: "it’s a good idea to use a package instead of a module".
1

The first parameter of redirect is the url which you want the client redirected to. You can hard code the url as the first parameter. And also you can use url_for to generate the url for the given endpoint.

For example:

@app.route("/url")
def endpoint():
    return "Test redirect & url_for"

You can use redirect(url_for("endpoint")) or redirect("/url") to redirect to /url

And the Quickstart point the advantages of use url_for instead of hard code the url.

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.