Reputation: 105
I've been creating a forum based learning system, similar to stack overflow for an assessed project. I'm fairly new to flask, however I believe i have a decent understanding of python. I have been following Corey Schafer's Flask tutorials and adapting them to my project. Whenever I try to access the page called 'adduser', a webpage with a form for adding users, I get the error:
"TypeError: hidden_tag() missing 1 required positional argument: 'self'".
I have no idea what this means or how to even attempt to fix it.
I assumed that I might find the fix in the HTML for the 'adduser' page, and after removing the '{{ form.hidden_tag() }}'
tag, I got a different error, which leads me to believe that the error is has something to do with the 'forms.py'
file and the 'addUser.html'
file.
forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from itroom.models import User
class LoginForm(FlaskForm):
email = StringField('Email',validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
remember = BooleanField('Remember Me')
submit = SubmitField('Login')
class AddUserForm(FlaskForm):
email = StringField('Email',validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired()])
submit = SubmitField('Add User')
def validate_email(self,email):
user = User.query.filter_by(userEmail=email.data).first()
if user:
raise ValidationError('Email already exists in database.')
addUser.html
{% extends "template.html" %}
{% block content %}
<div class="center2" style="border-color: white;">
<h2 align="center" style="padding: 2.5%;"></h2>
<h1>Add a User</h1>
<form method="POST" action="">
{{ form.hidden_tag() }}
<div class="form-group">
{{form.email.label}}
{% if form.email.errors %}
{{form.email(size=30, class="form-control is-invalid")}}
<div class="invalid-feedback">
{% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{form.email(size=30, class="form-control")}}
{% endif %}
</div>
{{form.password.label}} <br>
{{form.password(size=30)}}
{{form.submit(class="btn btn-outline-info")}} <br>
</form>
</div>
{% endblock content %}
I also thought the routes file may be helpful aswell.
routes.py
from flask import Flask, render_template, url_for, flash, redirect
from itroom import app, db, bcrypt
from itroom.forms import LoginForm, AddUserForm
from itroom.models import User, Post
from flask_login import login_user
@app.route("/")
@app.route("/home") #defines the HTML loaded for /home
def home():
return render_template('home.html', title='Home')
@app.route("/login", methods=['GET', 'POST']) #defines the HTML loaded for /login
def login():
form = LoginForm('')
if form.validate_on_submit():
user = User.query.filter_by(email=form.userEmail.data).first()
if user and bcrypt.check_password_hash(user.userPassword, form.password):
login_user(user, remember=form.remember.data)
return redirect(url_for('home'))
else:
flash('Login Unsucessful, Please check email and password')
return render_template('login.html',form=form, title='Login')
@app.route("/adduser", methods=['GET', 'POST']) #defines the HTML loaded for /adduser
def addUser():
form = AddUserForm('/login')
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode ('utf-8')
user = User(userEmail=form.email.data, userPassword = hashed_password)
db.session.add(user)
db.session.commit()
temptext = ("Account created for '", form.email.data, "'." )
flash(temptext)
else:
flash('Account not created')
return render_template('addUser.html', title='Admin', form=AddUserForm)
Thank you in advance for anyone who tries to help!
Upvotes: 4
Views: 6646
Reputation: 11
Well, me following the same tutorial from Corey Schafer's flask. The main reason why you are getting TypeError: hidden_tag() missing 1 required positional argument: 'self
is because of the route page form the bracket needs to prevent the error and to signify the tag function()
Upvotes: 1
Reputation: 69
You forgot to call the form at the end of your last return statement.
current code
else:
flash('Account not created')
return render_template('addUser.html', title='Admin', form=AddUserForm)
fixed code
else:
flash('Account not created')
return render_template('addUser.html', title='Admin', form=AddUserForm())
or since you already specified that
form = AddUserForm('/login')
just specify it at the end like you did in other routes
return render_template('addUser.html', title='Admin', form=form)
Upvotes: 5
Reputation: 1070
The TypeError is saying that self was not passed to the hidden_tag method. You can have this error if you do the following by accident:
class A:
def test(self):
print('test')
A.test() # TypeError: test() missing 1 required positional argument: 'self'
A().test() # prints test
So this means that you are calling the method with a class object.
In your routes, you have the following rule where you pass a class to your page, but you likely wanted to return your actual form object. So you have to change
return render_template('addUser.html', title='Admin', form=AddUserForm)
to
form = AddUserForm('/login')
...
return render_template('addUser.html', title='Admin', form=form)
Upvotes: 8