我正在尝试将内容添加到Flask-admin列表视图中。 我想在列表视图的顶部添加自己的内容。 到目前为止,我所做的就是扩展默认列表视图并添加了自己的内容,如下所示:
{% 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 %}
此模板的自定义模型视图为:
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)
现在我的问题是列表视图期望设置一定数量的参数(以处理显示列表)。
这些参数在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
我的问题是:如何将参数包含在父视图的render调用中?
谢谢!
答案 0 :(得分:0)
尝试覆盖视图的render
方法,您将可以在其中将变量注入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)