昨天试过可以 post 成功的,后面不知道做了什么改动,今天又不行了。大佬能否指教下。
'''Python
#post.html 文件:
<form
method="post"
action="{{ url_for('content.post', **request.args) }}"
role="form"
>
<legend>说点什么吧!</legend>
{{ form.csrf_token }}
<div>
{{ form.title(placeholder="标题",
type="text",
autofocus="true",
spellcheck="false") }}
</div>
<div>
{{ form.tags(placeholder="标签 /话题",
type="text") }}
</div>
<select name="category">
<option value="">选择分类</option>
{% for o in form.category.choices %}
{%- if o[2] == None -%}
<option value="{{ o[0] }}">{{ o[1] }}</option>
{% else %}
<option value="{{ o[0] }}">--{{ o[1] }}</option>
{%- endif %}
{% endfor %}
</select>
<div>
{{ form.content(placeholder="内容",
type="text") }}
</div>
<button type="submit">发布</button>
</form>
form 文件:
# POST 表单
class PostForm(FlaskForm):
title = StringField(
'title',
validators=[
DataRequired()
])
tags = StringField(
'tags'
)
category = SelectField(
'category',
coerce=int
)
content = TextAreaField(
'content',
validators=[
DataRequired()
])
#路由:
@
bp.route('/post', methods=['GET', 'POST'])
def post():
''' 发帖 '''
if g.user.is_authenticated:
form = PostForm()
# 分类处理
form.category.choices = [(obj.id, obj.name, obj.parent) for obj in Category.query.order_by('id')]
if form.validate_on_submit():
# 标签处理
tag_list = form.tags.data.split(',')
tags = []
for l in tag_list:
name = Lable.query.filter_by(name=l).first()
if name is None:
tags.append(Lable(name=l))
else:
tags.append(name.decode('utf-8'))
# 分类处理
category = Category.query.filter_by(id=form.category.data).first()
posts = PostContent(p_title=form.title.data,
p_lable=tags,
p_content=form.content.data,
p_category_name=category,
is_posted=True,
p_author=g.user.id
)
db.session.add(posts)
db.session.commit()
db.session.flush()
# return redirect('/')
else:
abort(404)
return render_template('/content/post.html', form=form)
'''