Get next URL using flask redirect - python

I am following this information here, all seems good, but I need to know how to load the originally requested url from the new login page on the second to last line.
#app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if not models.check_auth(username, password):
error = 'Please try again.'
else:
return redirect(url_for(**next**))
return render_template('login.html', error=error)

Try this out:
#app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if not models.check_auth(username, password):
error = 'Please try again.'
else:
return redirect(request.args.get("next") or url_for("admin"))
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return render_template('login.html', error=error)

Related

i'm trying to flash message to user, but nothing appears in my website, what is wrong in my code?

#app.route("/signin", methods=["GET", "POST"]) def signin(): #return render_template("signin.html") if request.method == "POST": email = request.form.get("email") password = request.form.get("password")

Login app using python flask not redirecting to login page

I am creating a login app using python flask and mongodb at the backend, everything is working fine but in login its not redirecting into login page.
#here is my code
#app.route('/login/', methods=["GET","POST"])
def login_page():
error = ''
try:
conn = connection()
collection=MongoClient()["blog"]["users"]
if request.method == "POST":
data = collection.find({"username":(request.form['username'])})
#data1=collection.find({"password":(request.form['password'])})
if sha256_crypt.verify(request.form.password,data):
print("password verification suceessful")
session['logged_in'] = True
session['username'] = request.form['username']
flash("You are now logged in")
return redirect(url_for("dashboard"))
else:
error = "Invalid credentials, try again."
gc.collect()
return render_template("login.html", error=error)
except Exception as e:
error = "Invalid credentials, try again."
return render_template("login.html", error = error)
Here, I am trying to verify the user-entered username and password is matching with my database collection. If matching, the user can login else throw one error messgae and redirect to login page. Can anyone help me out to solve the issue?
here is the answer for my question,
#app.route('/login/', methods=["GET","POST"])
def login_page():
error = ''
try:
conn = connection()
collection=MongoClient()["blog"]["users"]
if request.method == "POST":
#collection = MongoClient()["blog"]["users"]
data = collection.find_one({"username":(request.form['username'])})
#data1=collection.find({"password":(request.form['password'])}
#data = c.fetchone()[2]
password=request.form['password']
if sha256_crypt.verify(password,data['password']):
print("password verification suceessful")
session['logged_in'] = True
session['username'] = request.form['username']
flash("You are now logged in")
return redirect(url_for("dashboard"))
else:
error = "Invalid credentials, try again."
# gc.collect()
return render_template("login.html", error=error)
except Exception as e:
#flash(e)
error = "Invalid credentials, try again."
return render_template("login.html", error = error)

Not able to run this view function in python using flask framework

#app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if request.method == 'POST':
user = User.query.filter_by(username=form.name.data).first()
if user is None:
user = User(username=form.name.data)
db.session.add(user)
db.session.commit()
session['known'] = False
else:
session['known'] = True
session['name'] = form.name.data
form.name.data = ''
return redirect('/')
return render_template("index1.html", form=form, name=session.get('name'), known=session.get('known', False))
While running the following program once I am submitting the form its getting internal server error. Please help me in this, really struggling hard to rectify this.

Flask doubts regarding redirection

I am following this tutorial here. The login page shows a sign in page. When I click on any of the providers, and click on the submit button, the page gets redirected back to the login page. What am I doing wrong here? I have the following code in the views.py page
#app.route('/login', methods=['GET', 'POST'])
#oid.loginhandler
def login():
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
session['remember_me'] = form.remember_me.data
return oid.try_login(form.openid.data, ask_for=['nickname', 'email'])
return render_template('login.html',
title='Sign In',
form=form,
providers=app.config['OPENID_PROVIDERS'])
#oid.after_login
def after_login(resp):
if resp.email is None or resp.email=="":
flash('Invalid login. Please try again')
return redirect(url_for('login'))
user=User.query.filter_by(email=resp.email).first()
if user is None:
nickname= resp.nickname
if nickname is None or nickname == "":
nickname=resp.email.split('#')[0]
user= User(nickname=nickname, email=resp.email)
db.session.add(user)
db.session.commit()
remember_me=False
if 'remember_me' in session:
remember_me=session['remember_me']
session.pop('remember_me', None)
login_user(user, remember = remember_me)
return redirect(request.args.get('next') or url_for('index'))
you need to set the app config
app.config['SECURITY_POST_LOGIN_VIEW'] = '/post_login_page'
app.config['SECURITY_POST_REGISTER_VIEW'] = '/register_complete'

TypeError: redirect() got an unexpected keyword argument 'logout'

Prevents me from passing along the boolean logout to index. I used the exact same redirect format in another part of code, which worked.
#app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'user' or \
request.form['password'] != 'pass':
error = 'Invalid credentials'
else:
session['username'] = request.form['username']
flash('You are already logged in.')
logout = False
return redirect(url_for('index'), logout=logout) #problem line
return render_template('login.html', error=error)
You probably want to pass the logout value to url_for():
redirect(url_for('index', logout=logout))
However, in this case it'd be a good idea to convert it to int first - otherwise you end up with a literal False or True in the URL.

Categories

Resources