Flask - 未使用表单提交创建的数据库项

时间:2016-11-21 11:02:46

标签: python forms flask

我正在尝试让用户通过项目页面提交项目评论(而不是通过单独的评论路线)。

此代码位于我的__init__.py文件夹中。我有一个每个项目的路线,我重复注释功能的@app.route代码。但是,当我在模板上单击“提交”时,没有任何反应。

@app.route("/projects/<int:project_id>/", methods=['GET', 'POST'])
def project_detail(project_id):
    project = db.session.query(Project).get(project_id)
    all_comments = db.session.query(ProjectComment).filter_by(project_id=project_id)
    if project is None:
        abort(404)
    return render_template("project_detail.html", project=project, all_comments=all_comments)

@app.route("/projects/<int:project_id>/", methods=['GET', 'POST'])
def comment(project_id):
    project = db.session.query(Project).get(project_id)
    all_comments = db.session.query(ProjectComment).filter_by(project_id=project_id)
    if request.method == 'GET':
        return render_template("project_detail.html", project=project, all_comments=all_comments)
    elif request.method == 'POST':
        now = datetime.datetime.now()
        new_comment = ProjectComment(
            member_id = current_user.id,
            project_id = project_id,
            content = request.form.get("content"),
            #time_created = now
            )
        db.session.add(new_comment)
        db.session.commit()
        return render_template("project_detail.html", project=project, all_comments=all_comments)

我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

需要重复路由功能的代码。你可以让它成为一条路线并检查方法是获取还是发布。