gpt4 book ai didi

python - 扩展 Flask 管理模板时向渲染调用添加参数

转载 作者:行者123 更新时间:2023-12-05 01:41:25 24 4
gpt4 key购买 nike

我正在尝试将内容添加到 Flask-admin ListView 。我想在 ListView 的顶部添加我自己的内容。到目前为止我所做的是扩展默认 ListView 并添加我自己的内容,如下所示:

{% extends 'admin/model/list.html' %}

{% block body %}
<h3>Submit report</h3>
{% if report_form.errors %}
<ul class="errors">
{% for field_name, field_errors in report_form.errors|dictsort if field_errors %}
{% for error in field_errors %}
<li>{{ form[field_name].label }}: {{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endif %}
<form action="{{ url_for('report.index') }}" method="post" enctype="multipart/form-data">
{{ report_form.file }}
{{ report_form.csrf_token }}
<button type="submit">Send</button>
</form>
{{ super() }}
{% endblock %}

我为这个模板定制的模型 View 是:

class ReportAdmin(ModelView):
@expose('/', methods=('GET', 'POST'))
def index(self):
report_form = ReportFileForm()
if form.validate_on_submit():
file = form.file.data

#Check for report duplicate
if Report.query.filter(Report.filename == file.filename).all():
flash('Could not add report because a report with filename {} already exists.'.format(file.filename), 'error')
else:
try:
report = parser_factory(file)
flash('Report was submitted succesfully')
return redirect(url_for('report.index_view'))
except ValueError as e:
form.file.errors.append(e)

return self.render('report/index.html', report_form=report_form)

现在我的问题是 ListView 需要设置一定数量的参数(以处理显示列表)。

这些参数在base.py 中定义

return self.render(
self.list_template,
data=data,
form=form,
delete_form=delete_form,

# List
list_columns=self._list_columns,
sortable_columns=self._sortable_columns,
editable_columns=self.column_editable_list,

# Pagination
count=count,
pager_url=pager_url,
num_pages=num_pages,
page=view_args.page,

# Sorting
sort_column=view_args.sort,
sort_desc=view_args.sort_desc,
sort_url=sort_url,

# Search
search_supported=self._search_supported,
clear_search_url=clear_search_url,
search=view_args.search,

# Filters
filters=self._filters,
filter_groups=self._filter_groups,
active_filters=view_args.filters,

# Actions
actions=actions,
actions_confirmation=actions_confirmation,

# Misc
enumerate=enumerate,
get_pk_value=self.get_pk_value,
get_value=self.get_list_value,
return_url=self._get_list_url(view_args),
)

当然,当尝试显示页面时,我得到一个错误:

jinja2.exceptions.UndefinedError: 'num_pages' is undefined

我的问题是:如何将我的参数包含到父 View 的渲染调用中?

谢谢!

最佳答案

尝试覆盖 View 的 render 方法,您可以将变量注入(inject)到 kwargs 参数中。

示例(未经测试)

class ReportAdmin(ModelView):

list_template = 'report/index.html'

# Override to allow POSTS
@expose('/', methods=('GET', 'POST'))
def index_view(self):
return super(ReportAdmin, self).index_view(self)

def render(self, template, **kwargs):
# we are only interested in our custom list page
if template == 'report/index.html':

report_form = ReportFileForm()
if report_form.validate_on_submit():
file = report_form.file.data

#Check for report duplicate
if Report.query.filter(Report.filename == file.filename).all():
flash('Could not add report because a report with filename {} already exists.'.format(file.filename), 'error')
else:
try:
report = parser_factory(file)
flash('Report was submitted succesfully')
except ValueError as e:
report_form.file.errors.append(e)

kwargs['report_form'] = report_form

return super(ReportAdmin, self).render(template, **kwargs)

关于python - 扩展 Flask 管理模板时向渲染调用添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54443793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com