Python 表单:提交后如何刷新页面
我有一个显示预订信息列表的页面。有一个侧边栏,用户可以使用它来过滤特定的预订。一个元素是多复选框表单,用户可以在其中选择多个教室 用户提交表单后,将生成一个URL,其中包含新的筛选信息并重定向到URL 表格: 蓝图:Python 表单:提交后如何刷新页面,python,html,flask,flask-wtforms,Python,Html,Flask,Flask Wtforms,我有一个显示预订信息列表的页面。有一个侧边栏,用户可以使用它来过滤特定的预订。一个元素是多复选框表单,用户可以在其中选择多个教室 用户提交表单后,将生成一个URL,其中包含新的筛选信息并重定向到URL 表格: 蓝图: @resblueprint.route('/<resfilter:res_filter>/', defaults={'page': 1}, methods=['GET', 'POST']) @resblueprint.route(
@resblueprint.route('/<resfilter:res_filter>/', defaults={'page': 1},
methods=['GET', 'POST'])
@resblueprint.route('/<resfilter:res_filter>/<int:page>')
def allreservations(res_filter, page):
'''Display reservations'''
# code to generate other template parameters
# code to dynamically generate the choices: classrooms_list
form = classroomSidebarForm()
form.classrooms_list.choices = classrooms_list
if request.method == 'POST':
selected_classrooms = ()
if form.classrooms_list.data is not None:
for selected_classroom_id in form.classrooms_list.data:
selected_classrooms.append(
dict(form.classrooms_list.choices).selected_classroom_id)
newconds = (res_filter.conds[0], res_filter.conds[1], selected_classrooms,
res_filter.conds[3], res_filter.conds[4], res_filter.conds[5])
res_filter.conds = newconds
# the above updates the reservation filter
# where conds[2] is replaced by the updated list of classrooms
# then the same page should be rendered again, with the updated res_filter
# preferably the form is not cleared
return render_template('reservation/allres.html.j2',
res=res,
pagination=pagination,
res_filter=res_filter,
form=form)
@resblueprint.route('/',默认值={'page':1},
方法=['GET','POST'])
@resblueprint.route(“/”)
def所有预订(res_过滤器,第页):
“显示预订”
#生成其他模板参数的代码
#动态生成选项的代码:列表
form=classroomSidebarForm()
form.questions\u list.choices=教室\u列表
如果request.method==“POST”:
选定的教室=()
如果form.u list.data不是无:
对于form.Questions\u list.data中选定的\u教室\u id:
选定的\u.append(
dict(表格.教室\列表.选项).所选教室\ id)
newconds=(res_filter.conds[0],res_filter.conds[1],所选的,
res_filter.conds[3]、res_filter.conds[4]、res_filter.conds[5])
res_filter.conds=newconds
#上述操作将更新预订过滤器
#其中conds[2]被更新的教室列表所取代
#然后使用更新的res_过滤器再次呈现相同的页面
#最好不清除表格
返回render_模板('reservation/allres.html.j2',
res=res,
分页=分页,
res_filter=res_filter,
形式=形式)
侧栏:
{% macro desktop_sidebar_res(endpoint, res_filter, form) %}
<div id="sidebar" class="col-md-2">
# generates the other filter options
<form action="{{ url_for(endpoint, res_filter=res_filter.to_url()) }}" method="POST">
{{ form.csrf_token }}
{{ form.classrooms_list.label }} {{ form.classrooms_list }}
{{ form.submit }}
</form>
{{ caller and caller() }}
</div>
{% endmacro %}
{%macro desktop\u侧边栏\u res(端点,res\u过滤器,表单)%}
#生成其他过滤器选项
{{form.csrf_token}
{{form.questions_list.label}{{{form.questions_list}}
{{form.submit}
{{caller and caller()}}
{%endmacro%}
可以确认url\u for(endpoint,res\u filter=res\u filter.to\u url())
使用当前筛选器信息正确生成当前url
代码无法将表单数据返回blueprint函数。如果request.method='POST':之后的块无法访问。所以我怀疑action=“
可能有问题
生成新过滤器后。如何重新加载模板
如果需要进一步澄清,请告诉我。我正在处理一个现有项目。正如您所看到的,我是web开发的绝对初学者。欢迎任何指点!您的代码中是否有
DEBUG=True
?如果这是真的,您将在控制台中获得调试信息,并将帮助您解决问题。如果您收到错误,请共享回溯错误。存在418错误。我在uWSGI日志或浏览器控制台中没有看到错误。我认为您还应该使用if request.method=='GET'
来获取表单数据。而不是if request.method=='POST'
尝试使用if form.validate on_submit()
(它还将检查csrf)
{% macro desktop_sidebar_res(endpoint, res_filter, form) %}
<div id="sidebar" class="col-md-2">
# generates the other filter options
<form action="{{ url_for(endpoint, res_filter=res_filter.to_url()) }}" method="POST">
{{ form.csrf_token }}
{{ form.classrooms_list.label }} {{ form.classrooms_list }}
{{ form.submit }}
</form>
{{ caller and caller() }}
</div>
{% endmacro %}