我目前正在使用Flask,我一直遇到这个错误:
" profile_creation:缺少1个必需的位置参数:' user'"
所以,我查看了这个网站上的一些帖子,大部分答案都涉及添加request.args.get()
。所以,我尽我所能处理帖子并对我的代码进行了编辑,但我仍然得到了错误。这就是我现在所拥有的:
@app.route("/profile-creation", methods = ['GET', 'POST'])
def profile_creation(user):
user = request.args.get("x")
form = ProfileForm()
if form.validate_on_submit():
...
return redirect(url_for('/home'))
@app.route("/register", methods = ['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user = User(username = form.username.data, first_name = form.first_name.data, last_name = form.last_name.data,
email = form.email.data, password = hashed_password)
return redirect(url_for('profile_creation', x = user))
这两个页面都与用户填写个人信息表格(注册帐户,然后发布一些个人资料,如年龄等)有关。我在哪里错了?任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
如果user
位于/profile-creation?user=my_user
格式的网址中,则使用
@app.route("/profile-creation", methods = ['GET', 'POST'])
def profile_creation(): # Note: no user argument here
user = request.args.get("my_user")
如果相反,它就像/profile-creation/my_user
@app.route("/profile-creation/<user>", methods = ['GET', 'POST'])
def profile_creation(user):
# user argument will be set to 'my_user'