简体   繁体   English

如何放置一种格式,在该格式中分析用户上传的文件,然后将其显示在新的url上?

[英]How can I put in a form where user-uploaded files are parsed and then displayed on a new url?

Ultimately, I want it so that when users upload a file, it parses the file and then creates a new url (using save_url ) where it displays the output of that parsing. 最终,我想要它,以便当用户上传文件时,它解析该文件,然后创建一个新的url(使用save_url ),在其中显示该解析的输出。

Here is the edit.pt that renders the form: 这是呈现表单的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>

Here is the add_page section of views.py : 这是views.pyadd_page部分:

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

And when I try and go to http://localhost:6543/add_page/new (to add a new page with a new url), I get this error: 当我尝试转到http://localhost:6543/add_page/new (添加具有新url的新页面)时,出现此错误:

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

This error occurs on the line under def add_page(request): . def add_page(request):下的行上发生此错误。 I am formatting it like so as to go off of this tutorial . 我正在格式化它,以便从本教程开始

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: 您将数据(例如,文件解析的结果)保存到某个永久性存储(数据库,这将为您提供一些唯一的URI来寻址该文件(例如, /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() 

The new "page" stays permanently, so if you just visit /files/123123 in your browser you'll see the same page. 新的“页面”将永久保留,因此,如果您只是在浏览器中访问/files/123123 ,则会看到同一页面。

  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. 另外,您的视图会收到一条HTTP帖子,对数据进行处理,并且不像其他“常规”视图一样,返回HTML斑点,而不是重定向到另一个视图。 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. 例如,当表单未能通过验证并且您想重新显示该表单时,将使用此方法,但是当您不将数据保存到任何地方(因此基本上没有重定向到的URI)时,此方法也很有用。

     @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. 文件提交还有一个额外的麻烦-通常,如果不将文件存储在服务器上的临时位置而进行一些欺骗性操作,则通常无法重新显示带有填充的文件字段的表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM