我对 Python 还很陌生(老实说,我对一般编程也很陌生)。我目前正在制定一种待办事项列表,我需要它将待办事项放入适当的课程中(所有这些都与教育内容相关)。所以,问题很简单。我将其作为 Flask 驱动的路线:
@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
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()
为字段的 choice 属性构建元组列表course
in ToDoForm
。我需要将当前登录的老师的id传递给这个函数,以便它可以评估这位通过的老师是否有任何与他的id匹配的课程。我尝试使用current_user._get_current_object()
等等,但这只是给我带来了很多错误。
任何帮助、意见或建议将不胜感激。我真的希望我在这里所说的(以及我想要实现的目标)是可以理解的