简体   繁体   English

Python初学者问题-试图了解return语句

[英]Python beginner question - trying to understand return statement

Basically I want to return the contents of create_user in the register function to use to save to my database. 基本上,我想在register函数中返回create_user的内容,以用于保存到数据库中。 I am a complete beginner. 我是一个完整的初学者。 What am I misunderstanding? 我有什么误会?

def register():
    form = SignupForm(request.form)
    if request.method == 'POST' and form.validate():
    create_user = ({'username' : form.username.data, 'email' : form.email.data,
                        'password': form.password.data})
    flash('Thanks for registering')
    return create_user, redirect(url_for('loggedin.html'))
return render_template('get-started.html', form=form)

create_user = register()
doc_id, doc_rev = db.save(create_user)

I think you've lost some formatting somewhere. 我认为您在某处丢失了一些格式。 The first return statement should be indented far enough that it's inside the if block and the second return statement should line up with the if block. 第一个return语句应缩进足够远,使其位于if块内,第二个return语句应与if块对齐。 If validation succeeds then it returns the tuple create_user, redirect(url_for('loggedin.html')) , otherwise it returns render_template('get-started.html', form=form) . 如果验证成功,则返回元组create_user, redirect(url_for('loggedin.html')) ,否则返回render_template('get-started.html', form=form)

Your indenting is wrong; 您的缩进是错误的; you want: 你要:

def register():
    form = SignupForm(request.form)
    if request.method == 'POST' and form.validate():
        create_user = ({'username' : form.username.data, 'email' : form.email.data,
                            'password': form.password.data})
        flash('Thanks for registering')
        return create_user, redirect(url_for('loggedin.html'))
    return render_template('get-started.html', form=form)

Indenting delineates blocks of code. 缩进表示代码块。 You need to indent everything inside the function to show that it is the code corresponding with that function, and everything inside the if . 您需要缩进函数内的所有内容以表明它是与该函数相对应的代码,以及if所有内容。 You haven't indented for the if . 您尚未缩进if

Did you remember to import request? 您还记得导入请求吗? Not sure what you're using but it looks like Flask to me, and if so it's 'from flask import request'. 不确定您使用的是什么,但对我来说它看起来像Flask,如果是,那是“来自烧瓶导入请求”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM