我正在尝试让用户通过项目页面提交项目评论(而不是通过单独的评论路线)。
此代码位于我的__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)
我该如何做到这一点?
答案 0 :(得分:1)
您不需要重复路由功能的代码。你可以让它成为一条路线并检查方法是获取还是发布。