Python 如何将用户上传的文件解析后显示在新的url上?
最终,我希望它能够在用户上传文件时解析文件,然后创建一个新的url(使用Python 如何将用户上传的文件解析后显示在新的url上?,python,file-upload,url-routing,pyramid,Python,File Upload,Url Routing,Pyramid,最终,我希望它能够在用户上传文件时解析文件,然后创建一个新的url(使用save\u url),在那里显示解析的输出 以下是呈现表单的edit.pt: <form action="/add_page" method="post" accept-charset="utf-8" enctype="multipart/form-data"> <label for="stl">Stl</label> <input name="stl" type="file" v
save\u url
),在那里显示解析的输出
以下是呈现表单的edit.pt:
<form action="/add_page" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<label for="stl">Stl</label>
<input name="stl" type="file" value="" />
<input type="submit" value="submit"/>
</form>
当我尝试进入http://localhost:6543/add_page/new
(若要使用新url添加新页面),我遇到以下错误:
KeyError: "No key 'stl': Not a form request".
此错误发生在
def add_页面(请求)下的行中:
。我正在格式化它,这样就可以离开了 基本上有两种场景“在表单提交上保存数据后要做什么”:
/files/123123
)。然后您只需向该位置发出HTTP重定向:
@view_config(renderer="templates/form_view.pt")
def form_view(self):
if self.request.method == 'POST':
if _validation_passed(request):
new_url = _save_data(request)
return HTTPFound(new_url)
else:
return _render_form(values=request.POST, msg="Validation failed")
# We are a GET not a POST, render empty form
return _render_form()
/files/123123
,您将看到相同的页面
@view_config(renderer="templates/form_view.pt")
def form_view(self):
if self.request.method == 'POST':
if _validation_passed(request):
return _render_data(request)
else:
return _render_form(values=request.POST, msg="Validation failed")
# We are a GET not a POST, render empty form
return _render_form()
文件提交还有一个额外的复杂性-如果不在服务器上的临时位置存储文件,则通常无法在填充文件字段的情况下重新显示表单。基本上有两种情况“在表单提交上保存数据后要做什么”:
/files/123123
)。然后您只需向该位置发出HTTP重定向:
@view_config(renderer="templates/form_view.pt")
def form_view(self):
if self.request.method == 'POST':
if _validation_passed(request):
new_url = _save_data(request)
return HTTPFound(new_url)
else:
return _render_form(values=request.POST, msg="Validation failed")
# We are a GET not a POST, render empty form
return _render_form()
/files/123123
,您将看到相同的页面
@view_config(renderer="templates/form_view.pt")
def form_view(self):
if self.request.method == 'POST':
if _validation_passed(request):
return _render_data(request)
else:
return _render_form(values=request.POST, msg="Validation failed")
# We are a GET not a POST, render empty form
return _render_form()
文件提交还有一个额外的复杂问题-如果不在服务器上的临时位置存储文件,您通常无法在填充文件字段的情况下重新显示表单。请包含完整的回溯,这样我们就不必猜测哪里出了问题。您是指我的其他公开问题吗在…(分析数据)…部分。我意识到我的两个问题比我想象的要相似得多。有没有办法将它们合并?不太可能;你可以请求关闭这一个,作为另一个问题的副本(注意,选择其他,用另一个问题的链接解释)。请包含完整的回溯,这样我们就不必猜测哪里出了问题。您是指我的其他未决问题还是…(分析数据)…部分。我意识到我的两个问题比我想象的要相似得多。有没有办法将它们合并?没有;你可以请求将这一个作为另一个问题的副本关闭(注意,选择其他,用另一个问题的链接解释)。