I am using Flask to create a /register before call the /login page.
This error show when I submit my register and go to ‘/login’.
This is my ‘app.py’:
from projeto import app, db from flask import Blueprint, render_template, redirect, request, url_for, flash, abort from flask_login import login_user, login_required, logout_user from projeto.models import User from projeto.forms import LoginForm, RegistrationForm @app.route('/') def home(): return render_template('home.html') @app.route('/welcome') @login_required def welcome_user(): return render_template('welcome_user.html') @app.route('/logout') @login_required def logout(): logout_user() flash('Logout realizado com sucesso!') return redirect(url_for('home')) @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): # Grab the user from our User Models table user = User.query.filter_by(email=form.email.data).first() # Check that the user was supplied and the password is right # The verify_password method comes from the User object # https://stackoverflow.com/questions/2209755/python-operation-vs-is-not if user.check_password(form.password.data) and user is not None: #Log in the user login_user(user) flash('Login realizado com sucesso!') # If a user was trying to visit a page that requires a login # flask saves that URL as 'next'. next = request.args.get('next') # So let's now check if that next exists, otherwise we'll go to # the welcome page. if next == None or not next[0]=='/': next = url_for('welcome_user') return redirect(next) return render_template('login.html', form=form) @app.route('/register', methods=['GET', 'POST']) def register(): form = RegistrationForm() if request.method == 'POST': user = User(name=form.name.data, address=form.address.data, number=form.number.data, city=form.city.data, state=form.state.data, country_code=form.country_code.data, phone=form.phone.data, email=form.email.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() flash('Obrigado por seu registro! Agora você pode realizar o login.') return redirect(url_for('login')) return render_template('register.html', form=form) if __name__ == '__main__': app.run(debug=True)
This is my ‘init.py’
import os from flask import Flask, Blueprint from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager #CREATE A LOGIN MANAGER OBJECT login_manager = LoginManager() #CREATE MY APPLICATION app = Flask(__name__) # CREATE MY DATABASE app.config['SECRET_KEY'] = 'secret' basedir = os.path.abspath(os.path.dirname(__file__)) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False #CONFIGURATE MY DATABASE WITH THE APP STRUCTURE db = SQLAlchemy(app) Migrate(app,db) #Pass in our app to the login MANAGER login_manager.init_app(app) # Tell users what view to go when they need to login_manager login_manager.login_view = 'login'
This is my ‘models.py’
from projeto import db, login_manager from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin @login_manager.user_loader def load_user(user_id): return User.query.get(user_id) class User(db.Model, UserMixin): # Create a table in the db __tablename__ = 'usuario' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(70)) address = db.Column(db.String(120)) number = db.Column(db.Integer) city = db.Column(db.String(150)) state = db.Column(db.String(20)) country_code = db.Column(db.Integer) phone = db.Column(db.String(11)) email =db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) def __init__(self, name, address, numer, city, state, country_code, phone, email, username, password): self.name = name self.address = address self.number = number self.city = city self.state = state self.country_code = country_code self.phone = phone self.email = email self.username = username self.password = password
This is the “Traceback”
TypeError TypeError: __init__() got an unexpected keyword argument 'number' Traceback (most recent call last) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflask_compat.py", line 39, in reraise raise value File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflask_compat.py", line 39, in reraise raise value File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:UsersSony_anaconda3envsrodrigovirtualLibsite-packagesflaskapp.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:UsersSonyDesktopProjeto_Flaskapp.py", line 63, in register user = User(name=form.name.data, TypeError: __init__() got an unexpected keyword argument 'number'
I just wanna understand my problem to solve it.
I tried to search this problem, finding some cases, but still cannot figure out how to solve it.
Advertisement
Answer
Simple answer.
You have a typo in your model’s init method.
You wrote numer
instead of number
.