错误:"TypeError:hidden_tag()缺少1个必需的位置参数:'self'"在Flask,Python中 [英] Error: "TypeError: hidden_tag() missing 1 required positional argument: 'self' " in Flask, python
问题描述
我一直在创建一个基于论坛的学习系统,类似于评估项目的堆栈溢出.我对flask非常陌生,但是我相信我对python有一个不错的了解.我一直在关注 Corey Schafer's Flask教程并将其适应我的项目.每当我尝试访问名为"adduser"的页面(带有添加用户表单的网页)时,都会收到错误消息:
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'".
我不知道这意味着什么,甚至不知道如何解决它.
我以为我可以在HTML中找到"adduser"页面的修复程序,并且在删除'{{ form.hidden_tag() }}'
标记后,我遇到了另一个错误,这使我相信该错误与'addUser.html'
文件.
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
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
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
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!
推荐答案
TypeError表示自身未传递给hidden_tag方法.如果您不小心执行以下操作,则可能会出现此错误:
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)
到
form = AddUserForm('/login')
...
return render_template('addUser.html', title='Admin', form=form)
这篇关于错误:"TypeError:hidden_tag()缺少1个必需的位置参数:'self'"在Flask,Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!