Forums

'from flask import *' used; unable to detect undefined names

Hi I get an error message on the following code:

'from flask import *' used; unable to detect undefined names

What could be causing this error? The log file state " SyntaxError: invalid syntax" the code is:

from flask import *

app = Flask(__name__)

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

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

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


@app.route('/log', methods=['GET', 'POST'])
def log ():
    error = None
    if request.method == 'POST':
            if request.form['username'] != 'admin' or request.form['password'] != 'admin':
                error = 'Invalid credentials, please try again.'
            else:
                return redirect(url_for('hello'))
    return render_template('log.html', error=error)

if __name__ == "__main__":
    app.run()

Try to use:

from flask import Flask

Regards.

That sounds like it's two errors; the first one (which I guess you're seeing in the in-browser editor) is just a warning that if you use from XXX import * then it can't check your code for undefined name errors. moisessantiago's suggestion of doing an explicit import is a good one -- if you do that then the error-checking will be better.

The syntax error is probably a separate problem; I see that your site is up and running right now, so perhaps you worked out what it was and fixed it?

Thanks for your support and suggestions! My site is indeed up and running but the error message persists in the in-browser editor while I didn't fix anything else. I tried Moissessantiago's suggestion but this results in error multiple messages about undefined names. Guess I leave the from flask import *

Does this work?

from flask import Flask, url_for, render_template, request, redirect

Your HTML templates also need to be in a folder named "templates".

After taking a closer look there are two more things to point out:

1) It looks like there is a space between "log" and "()". Should be def log():

2) Try adding a period in front of the function name in url_for in your last route like this return redirect(url_for('.hello')) It can also be written as return redirect(url_for('app.hello'))

I copy-paste your example plus this modification:

from flask import Flask, redirect, render_template, request, url_for

it's works!

http://moisessantiago.pythonanywhere.com/

Regards

:)

Thanks Leapfrog, excellent catches!

Happy to pay it forward