'TypeError: get_id() missing 1 required positional argument: 'self' in Flask
im trying to create login function (without registration)on my own website and im getting this error:
TypeError: get_id() missing 1 required positional argument: 'self'
I've tried to google this, but i didn't find the working anwser.
app.py
from flask import Flask, render_template, request, url_for, flash, redirect
import os
from flask import send_from_directory
from flask_wtf import FlaskForm
from flask_login import UserMixin, login_user, LoginManager, login_required,logout_user,current_user
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired,Length,ValidationError
choices=[('1', 'M'), ('2', "F")]
SECRET_KEY = os.urandom(32)
app = Flask(__name__)
app.config['SECRET_KEY'] = SECRET_KEY
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
@login_manager.user_loader
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')
class LoginForm(FlaskForm):
login_username = StringField(validators=[InputRequired(), Length(min=4, max=20)],render_kw={"placeholder":"username"})
password = PasswordField(validators=[InputRequired(), Length(min=4, max=20)], render_kw={"placeholder":"password"})
submit = SubmitField("Login")
class registrationForm(FlaskForm):
login_username = StringField(validators=[InputRequired(), Length(min=4, max=20)],render_kw={"placeholder":"username"})
password = PasswordField(validators=[InputRequired(), Length(min=4, max=20)], render_kw={"placeholder":"password"})
class User(FlaskForm, UserMixin):
login_username = "12345"
password = "12345"
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
print(form.validate_on_submit())
if form.validate_on_submit():
login_user(User)
redirect('user_profile')
#flash('Logged in successfully.')
return render_template('login.html', form=form)
@app.route('/user_profile', methods=['GET', 'POST'])
@login_required
def user_profile():
return render_template('user_profile.html')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon/favicon.ico',mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
app.run(host="192.168.1.124",port=80,debug=True)
Here is login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" sizes="32x32" href="static/favicon/favicon.ico" />
<link rel="manifest" href="static/favicon/site.webmanifest">
<title>Login Page with Username and Password Example</title>
<link
href="https://fonts.googleapis.com/css?family=Open+Sans:400,700"
rel="stylesheet"
/>
<link rel="stylesheet" href="static\style\style.css" />
<script
type="text/javascript"
src="https://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=GA84mroqLJ3kxRYkjcFaFrPZ23jD8e1QN0kMVKLqcSVp_du2wwu0l_8AQBYBmRK9IUJKE_ZBwKS0oHvqtiME9nl4nW3Xk9294TXik7puZ3VaJwMEy4-b9lw5IVCcm8O5"
charset="UTF-8"
></script>
</head>
<body>
<body class="align">
<div class="grid">
<form action="#" method="POST" class="form login">
<div class="form__field">
<label for="login__username"
><svg class="icon">
<use
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="#user"
></use></svg
><span class="hidden">Username</span></label
>
{{ form.hidden_tag() }}
{{ form.login_username }}
</div>
<div class="form__field">
<label for="login__password"
><svg class="icon">
<use
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="#lock"
></use></svg
><span class="hidden">Password</span></label
>
{{ form.password }}
</div>
<div class="form__field">
{{ form.submit}}
</div>
</form>
</div>
<svg xmlns="http://www.w3.org/2000/svg" class="icons">
<symbol id="arrow-right" viewBox="0 0 1792 1792">
<path
d="M1600 960q0 54-37 91l-651 651q-39 37-91 37-51 0-90-37l-75-75q-38-38-38-91t38-91l293-293H245q-52 0-84.5-37.5T128 1024V896q0-53 32.5-90.5T245 768h704L656 474q-38-36-38-90t38-90l75-75q38-38 90-38 53 0 91 38l651 651q37 35 37 90z"
/>
</symbol>
<symbol id="lock" viewBox="0 0 1792 1792">
<path
d="M640 768h512V576q0-106-75-181t-181-75-181 75-75 181v192zm832 96v576q0 40-28 68t-68 28H416q-40 0-68-28t-28-68V864q0-40 28-68t68-28h32V576q0-184 132-316t316-132 316 132 132 316v192h32q40 0 68 28t28 68z"
/>
</symbol>
<symbol id="user" viewBox="0 0 1792 1792">
<path
d="M1600 1405q0 120-73 189.5t-194 69.5H459q-121 0-194-69.5T192 1405q0-53 3.5-103.5t14-109T236 1084t43-97.5 62-81 85.5-53.5T538 832q9 0 42 21.5t74.5 48 108 48T896 971t133.5-21.5 108-48 74.5-48 42-21.5q61 0 111.5 20t85.5 53.5 62 81 43 97.5 26.5 108.5 14 109 3.5 103.5zm-320-893q0 159-112.5 271.5T896 896 624.5 783.5 512 512t112.5-271.5T896 128t271.5 112.5T1280 512z"
/>
</symbol>
</svg>
</body>
</body>
</html>
user_profile.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
</head>
<body>
user profile
</body>
</html>
Can you tell me what im doing wrong here?
Solution 1:[1]
The UserMixin class in the Flask-Login package has an id field. This id value is accessed with the get_id() method.
def get_id(self):
try:
return str(self.id)
except AttributeError:
raise NotImplementedError("No `id` attribute - override `get_id`") from None
Please define a unique id value for your User object that inherit the UserMixin class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fatih |
