Tumgik
#txtusername
codehunter · 2 years
Text
flask-sqlalchemy - User Query 'BaseQuery' object has no attribute 'password'
I'm new to Python and Flask.
I'm following this tutorial http://douglasstarnes.com/index.php/2015/05/27/easy-authentication-with-flask-login/ to have registration and login pages and have slightly modified it to hash the passwords on registration and to verify the password against the hash on login.
Initial password registration hashing works, but verifying the hashed password stored in the database against the one given in plain text via a login form does not.
The error I am receiving is against the following line on /login page and for the following reason:
if user.count() == 1 and check_password_hash(user.password, password) == True:AttributeError: 'BaseQuery' object has no attribute 'password'
I cannot work out why I'm receiving this error. The user gets successfully queried from the database, and the user has it's hashed password within the password column.
The query I'm using and method of returning data from the password column are similar to that included in the documentation http://flask-sqlalchemy.pocoo.org/2.1/queries/#querying-records
This is my views.py (/login and the erroring line are towards the bottom)
from flask import Flask, render_template, request, abort, redirect, url_for, flashfrom flask.ext.login import LoginManager, UserMixin, login_user, logout_user, login_requiredfrom flask.ext.sqlalchemy import SQLAlchemyfrom werkzeug.security import generate_password_hash, check_password_hashfrom app import appimport oslogin_manager = LoginManager(app)login_manager.init_app(app)login_manager.login_view = 'login'login_manager.session_protection = "strong"db = SQLAlchemy(app)class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String) password = db.Column(db.String)@login_manager.user_loaderdef user_loader(user_id): user = User.query.filter_by(id=user_id) if user.count() == 1: return user.one() return [email protected]_first_requestdef init_request(): db.create_all()@app.route('/secret')@login_requireddef secret(): return render_template('secret.html')@app.route('/logout')def logout(): logout_user() return redirect(url_for('index'))@app.route('/register', methods=['GET', 'POST'])def register(): if request.method == 'GET': return render_template('register.html') elif request.method == 'POST': username = request.form['txtUsername'] password = request.form['txtPassword'] user = User.query.filter_by(username=username) if user.count() == 0: hashed_password = generate_password_hash(password) user = User(username=username, password=hashed_password) db.session.add(user) db.session.commit() flash('You have registered the username {0}. Please login'.format(username)) return redirect(url_for('login')) else: flash('The username {0} is already in use. Please try a new username.'.format(username)) return redirect(url_for('register')) else: abort(405)@app.route('/login', methods=['GET', 'POST'])def login(): if request.method == 'GET': return render_template('login.html', next=request.args.get('next')) elif request.method == 'POST': username = request.form['txtUsername'] password = request.form['txtPassword'] user = User.query.filter_by(username=username) if user.count() == 1 and check_password_hash(user.password, password) == True: login_user(user.one(), remember=True) flash('Welcome back {0}'.format(username)) try: next = request.form['next'] return redirect(next) except: return redirect(url_for('index')) else: flash('Invalid login') return redirect(url_for('login')) else: return abort(405)@app.route('/')def index(): return render_template('index.html')
Does anyone know why I cannot access the password column for the user, or in fact any column? I've tried all 3 within the database, ID, username and password.
https://codehunter.cc/a/flask/flask-sqlalchemy-user-query-basequery-object-has-no-attribute-password
0 notes
arthurknopper · 6 years
Text
Editable Text Field inside Alert Controller iOS Tutorial
Using the UIAlertController class it is not only possible to show alerts, but also get text input using Text Fields. In this tutorial a username and password is obtained from the user and displayed in the console. This tutorial is made with Xcode 10 and built for iOS 12.
Open Xcode and create a new Single View App.
For product name, use IOSTextFieldAlertControllerTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.
Go to the Storyboard. Drag a Button from the Object Library to the top of the main View. Double-click the Button and set the title to "Log in". Ctrl + Drag to the top of the main view and select the "Vertical Spacing to Top Layout Guide" and "Center Horizontally in Container". options.
The Storyboard should look like this.
Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Button and create the following Action.
Inside the ViewController class, implement the showAlertController action method.
@IBAction func showAlertController(_ sender: Any) { // 1. var usernameTextField: UITextField? var passwordTextField: UITextField? // 2. let alertController = UIAlertController( title: "Log in", message: "Please enter your credentials", preferredStyle: .alert) // 3. let loginAction = UIAlertAction( title: "Log in", style: .default) { (action) -> Void in if let username = usernameTextField?.text { print(" Username = \(username)") } else { print("No Username entered") } if let password = passwordTextField?.text { print("Password = \(password)") } else { print("No password entered") } } // 4. alertController.addTextField { (txtUsername) -> Void in usernameTextField = txtUsername usernameTextField!.placeholder = "<Your username here>" } alertController.addTextField { (txtPassword) -> Void in passwordTextField = txtPassword passwordTextField?.isSecureTextEntry = true passwordTextField?.placeholder = "<Your password here>" } // 5. alertController.addAction(loginAction) present(alertController, animated: true, completion: nil) }
Two optional variables are declared to represent the two textfields inside the Alert
An Alert Controller with the Alert style is created
An Alert Action is created, inside the closure the entered text in the Text Fields will be printed to the console.
The addTextField method adds the text input fields, taking the Text Field as a parameter inside the closure.
The login action is added to the Alert Controller and the controller is displayed.
Build and Run the project. Select the login button and fill in the username/password field inside the Alert Controller. The entered text will be displayed in the console.
You can download the source code of the IOSTextFieldAlertControllerTutorial at the ioscreator repository on Github.
0 notes
myquestionbank · 7 years
Text
ASP.NET (MCQ) questions and answers
1) If a user wants to create controls at runtime which event should be used to write code? a. PreLoad b. Load c. Init d. PreInit Answer Explanation ANSWER: PreInit 2) How will you add a TextBox control at runtime on the form? Choose the correct one. a. TextBox obj = new TextBox(); obj.ID = “txtUserName”; form1.Controls.Add(obj); b. form1.Controls.Add(TextBox); c. this.FindControl.add(TextBox); d.…
View On WordPress
0 notes
myquestionbank · 7 years
Text
.NET Test Questions Set
.NET Test Questions Set
1) If a user wants to create controls at runtime which event should be used to write code? a. PreLoad b. Load c. Init d. PreInit Answer Explanation ANSWER: PreInit Explanation: No explanation is available for this question! 2) How will you add a TextBox control at runtime on the form? Choose the correct one. a. TextBox obj = new TextBox(); obj.ID = “txtUserName”; form1.Controls.Add(obj); b.…
View On WordPress
0 notes