我正在玩烧瓶和烧瓶 - appbuilder模块,我有一个问题,我无法解决。我在views.py中有这段代码:
from flask.ext.appbuilder import AppBuilder, BaseView, expose, has_access, SimpleFormView
from flask_appbuilder._compat import as_unicode
from app import appbuilder
from wtforms import Form, StringField, BooleanField, TextField, SelectMultipleField
from wtforms.validators import DataRequired
from flask_appbuilder.fieldwidgets import BS3TextFieldWidget, DatePickerWidget
from flask.ext.appbuilder.forms import DynamicForm
from flask.ext.babelpkg import lazy_gettext as _
from wtforms.fields.html5 import DateField
from flask import flash, send_file, redirect, url_for
from scripts.create_excel import excelstuff
class MyView(BaseView):
default_view = 'method1'
@expose('/method1/')
@has_access
def method1(self):
# do something with param1
# and return to previous page or index
return 'Hello'
@expose('/method2/<string:param1>')
@has_access
def method2(self, param1):
# do something with param1
# and render template with param
param1 = 'Goodbye %s' % (param1)
return param1
@expose('/method3/<string:param1>')
@has_access
def method3(self, param1):
# do something with param1
# and render template with param
param1 = 'Goodbye %s' % (param1)
self.update_redirect()
return self.render_template('method3.html',
param1=param1)
@expose('/download/<string:filename>')
@has_access
def download(filename):
return send_file(appbuilder.app.config['UPLOAD_FOLDER'] + filename,
as_attachment=True)
# Forms
class MyForm(DynamicForm):
field1 = StringField(('Field1'),
description=('Your field number one!'),
validators=[DataRequired()], widget=BS3TextFieldWidget())
field2 = StringField(('Field2'),
description=('Your field number two!'), widget=BS3TextFieldWidget())
start = DateField('Start Date', format='%Y-%m-%d', validators=[DataRequired()], widget=DatePickerWidget())
end = DateField('Start Date', format='%Y-%m-%d', validators=[DataRequired()], widget=DatePickerWidget())
project1 = SelectMultipleField('Projects', choices=[('1', 'Choice1'), ('2', 'Choice2'), ('3', 'Choice3')],
coerce=unicode, validators=[DataRequired()])
username = StringField('Username', validators=[DataRequired()], widget=BS3TextFieldWidget())
class MyFormView(SimpleFormView):
default_view = 'this_form_get'
form = MyForm
form_title = 'This is my first form view'
message = 'My form submitted'
def form_post(self, form):
# process form
flash(as_unicode(form.field1.data), 'info')
excelstuff(as_unicode(form.field1.data))
return redirect(url_for('myview.download', filename='demo.xlsx'))
appbuilder.add_view(MyFormView, "My form View", icon="fa-group", label=_('My form View'),
category="My Forms", category_icon="fa-cogs")
appbuilder.add_view(MyView, "Method1", category='My View')
appbuilder.add_link("Method2", href='/myview/method2/john', category='My View')
appbuilder.add_link("Method3", href='/myview/method3/john', category='My View')
我发送了我的表单并执行了另一个python函数,它创建了一个包含表单发送内容的xls文件,但我不知道如何重定向到这个xls文件并下载它,这是具体的编码:
def form_post(self, form):
# process form
flash(as_unicode(form.field1.data), 'info')
excelstuff(as_unicode(form.field1.data))
return redirect(url_for('myview.download', filename='demo.xlsx'))
有人可以帮助我吗?
答案 0 :(得分:0)
你有两个问题。
1 - url_for是区分大小写的,所以这样做:
return redirect(url_for('MyView.download', filename='demo.xlsx'))
2 - SimpleFormView.form_post仅用于表单处理,答案由框架做出,所以在这种情况下你应该覆盖'this_form_post'
@expose("/form", methods=['POST'])
@has_access
def this_form_post(self):
self._init_vars()
form = self.form.refresh()
if form.validate_on_submit():
flash(as_unicode(form.field1.data), 'info')
excelstuff(as_unicode(form.field1.data))
return redirect(url_for('MyView.download', filename='demo.xlsx'))
else:
widgets = self._get_edit_widget(form=form)
return self.render_template(
self.form_template,
title=self.form_title,
widgets=widgets,
appbuilder=self.appbuilder
)
我没有测试过,告诉我它是否有用。