1

最终,我想要它,以便当用户上传文件时,它会解析文件,然后创建一个新的 url(使用save_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>

以下是 的add_page部分views.py

@view_config(route_name='add_page', renderer='templates/edit.pt')
def add_page(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')]

    ordering=[]
    N=len(normals)

    ...(parsing data)...


    return data
    if data is None:
        displayNotification['please upload file']
    if 'stl' in request.params:
        name=request.params('name')
        page=Page(name,data)
        return HTTPFound(location=request.route_url('view_page',pagename=name))
    save_url=request.route_url('add_page',pagename=name)
    page=Page('','')
    return dict(page=page,save_url=save_url)

当我尝试转到http://localhost:6543/add_page/new(添加带有新网址的新页面)时,我收到此错误:

KeyError: "No key 'stl': Not a form request".

此错误发生在def add_page(request):. 我正在格式化它,以便脱离本教程

4

1 回答 1

1

There are basically two scenarios of "what to do after you saved data on form submit":

  1. you save the data (say, the result of the file parsing) to some permanent storage (database, which will give you some unique URI to address that file (say, /files/123123). Then you just issue an HTTP redirect to that location:

    @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()
    

The new "page" stays permanently, so if you just visit /files/123123 in your browser you'll see the same page.

  1. Alternatively, your view receives an HTTP post, does something with the data and, instead of redirecting to another view, just returns a blob of HTML just like any other "normal" view. This is used when, say, the form fails validation and you want to re-display the form, but is also useful when you do not save the data anywhere, so basically you don't have an URI to redirect to.

    @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()
    

In this case, the result page will only be visible after the file is submitted, to view it again the user will need to re-upload the file.

Just follow one of those patterns and you'll be able to sort the things out.

There is an additional complication with file submits - you generally can't re-display the form with the file field populated without doing some trickery with storing a file in a temporary location on the server.

于 2013-01-18T21:33:39.597 回答