获取Flask中的当前用户ID [英] Get current user id in Flask

查看:444
本文介绍了获取Flask中的当前用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手(老实说,是一般的编程).我目前正在制定一种待办事项清单,在该清单中,我需要将待办事项放入适当的课程中(所有这些都与教育相关).因此,问题很直接.我将其作为Flask驱动的路线:

I'm quite new to Python (and, to be honest, programming in general). I'm currently working on a kind of to-do list, where I need it to put to-do items into appropriate course (it's all related to educational stuff). So, the problem is quite straight-forward. I have this as a Flask-driven route:

@app.route('/add_course', methods=('GET', 'POST'))
@login_required
def course():
    form = forms.CourseForm()
    if form.validate_on_submit():
        models.Course.create(teacher=g.user._get_current_object(),
                             name=form.name.data.strip(),
                             difficulty=form.level.data.strip(),
                             description=form.description.data.strip())
        flash("Course successfully created!", "success")
        return redirect(url_for('index'))
    return render_template('add_course.html', form=form)

然后我在forms.py中拥有它.您可以看到我为指示问题出在哪里的巧妙标记.它说THE_PROBLEM

Then I have this in forms.py. You can see the clever mark that I have put to indicate where my problem is. It says THE_PROBLEM

def courses():
    try:
        courses = models.Course.select(models.Course.id,
                                   models.Course.name,
                                   models.Course.difficulty).where(THE_PROBLEM)
        course_list = []
        for course in courses:
            course_list.append((str(course.id), course.name + ' - ' + course.difficulty.title()))
        return course_list
    except models.DoesNotExist:
        return [('', 'No courses')]


class ToDoForm(FlaskForm):
    name = StringField("What's up?", validators=[
    DataRequired()
    ])
    due_date = DateTimeField('When?', format='%Y-%m-%d %H-%M')
    course = SelectField('Course', choices=courses())
    priority = SelectField('Priority',
                           choices=[('high', 'High priority'),
                                    ('medium', 'Normal priority'),
                                    ('low', 'Low priority')],
                           validators=[
                               DataRequired()
                           ])
    description = TextAreaField('Description')

所以,是的,我要寻找的是传递当前已登录的所有者(在这种情况下为老师)的ID的方法.我使用此功能courses()来为ToDoForm中的字段course的choices属性.我需要将当前登录教师的ID传递给此函数,以便它可以评估该通过的教师是否有与其ID相匹配的课程.我尝试使用current_user._get_current_object()以及其他任何功能,但这只会给我带来很多错误.

So, yeah, what I am looking for is the way to pass the id of the owner (teacher in this very case) who is currently logged in. I use this function courses() to build a list of tuples for the choices attribute of the field course in ToDoForm. I need to pass the id of currently logged-in teacher into this function, so that it can evaluate if this passed teacher has any courses that match his id. I tried to use current_user._get_current_object() and whatever else, but it just caused me tons of errors.

任何帮助,建议或建议将不胜感激.我真的希望我在这里所说的话(以及我想实现的目标)是可以理解的

Any help, advice or suggestion will be appreciated. I really hope that what I say here (and what I want to achieve) is understandable

推荐答案

万一有人遇到同样令人沮丧的问题,解决方案将非常简单.将外部类包装到函数中,并将所需的变量作为参数传递,然后将它们用于您的内心. 只是不要忘记返回该类的实例.

In case anybody runs into the same frustrating problem, the solution is quite straightforward. Wrap up external classes into functions and pass required variables as arguments, then use them to your heart's content. Just don't forget to return an instance of the class.

这篇关于获取Flask中的当前用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆