现在我有这个表格:
<form action="/store_stl_data" method="post" accept-charset="utf-8"
enctype="multipart/form-data">
<label for="stl">STL</label>
<input id="stl" name="stl" type="file" value="" />
<input type="submit" value="submit" />
</form>
然后在我的views.py
我有
@view_config(route_name='store_stl_data', renderer='templates/edit')
def store_stl_data(request):
input_file=request.POST['stl'].file
i1, i2 = itertools.tee(input_file)
vertices = [map(float, line.split()[1:4])
for line in i1
if line.lstrip().startswith('vertex')]
normals = [map(float, line.split()[2:5])
for line in i2
if line.lstrip().startswith('facet')]
...(parsing data)...
return data
下面的三行def store_stl_data(request):
是我最不确定的。我从本教程中得到了它们。
我想要这样当人们上传文件时,整个store_stl_data
函数运行并处理输入文件。
现在它给了我一个错误:
KeyError: "No key 'stl': Not a form request"
这也是我的路线,在__init__.py
:
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import (
DBSession,
Base,
)
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('view_wiki', '/')
config.add_route('view_page', '/{pagename}')
config.add_route('add_page', '/add_page/{pagename}')
config.add_route('edit_page', '/{pagename}/edit_page')
config.scan()
return config.make_wsgi_app()