问题:管理员登录后 想要隐藏的评论表单字段无法隐藏
1.这是管理员的评论表单图,前三个想要隐藏,但隐藏不了
2.这是表单类:
# 游客评论表单
class CommentForm(FlaskForm):
author = StringField("姓名/昵称",validators=[DataRequired(message="无效数据"),Length(1,20,message="姓名或昵称长度在1到20位之间")],render_kw={"placeholder":"请输入姓名/昵称"})
email = StringField("邮箱",validators=[DataRequired(message="无效数据"),Length(1,254,message="邮箱长度不符"),Email(message="邮箱格式错误")],render_kw={"placeholder":"请输入邮箱"})
site = StringField("网址",validators=[Optional(),Length(0,254,message="网址长度过长"),URL("网址格式错误")],render_kw={"placeholder":"请输入网址(可以不填)"})
body = TextAreaField("评论内容",validators=[DataRequired("数据无效")])
submit = SubmitField("发表")
# 管理员评论表单
class AdminCommentForm(CommentForm):
author = HiddenField()
email = HiddenField()
site = HiddenField()
3.这是视图函数
```
# 发表评论
if current_user.is_authenticated:
comment_form = AdminCommentForm()
reviewed = True
from_admin = True
else:
comment_form = CommentForm()
reviewed = False
from_admin = False
if comment_form.validate_on_submit():
author = comment_form.author.data
email = comment_form.email.data
site = comment_form.site.data
body = comment_form.body.data
comment = Comment(author=author,email=email,site=site,body=body,reviewed=reviewed,from_admin=from_admin,post_id=post_id)
db.session.add(comment)
db.session.commit()
return redirect(url_for("blog.post",post_id=post_id))
return render_template("blog/post.html",post=post,pagination=pagination,comments=comments,comment_form=comment_form)
4.这是前端
<form action="{{ url_for("blog.post",post_id=post.id) }}" method="post" style="width:500px;">
{{ comment_form.csrf_token }}
{{ field_form(comment_form.author,type="text" ,class="form-control") }}
{{ field_form(comment_form.email,type="text" ,class="form-control") }}
{{ field_form(comment_form.site,type="text" ,class="form-control") }}
{{ field_form(comment_form.body,class="form-control",rows="3") }}
{{ field_form(comment_form.submit,type="submit",class="btn btn-default") }}
</form>