简体   繁体   English

附加到response.out.write的不需要的模板代码

[英]Unwanted template code attached to response.out.write

I'm stuck with trying to create a downloadable content. 我坚持尝试创建可下载的内容。 I'm using webapp2.RequestHandler and I've got the following scenario: 我正在使用webapp2.RequestHandler并且出现以下情况:

  1. I'm using Jinja 2 to create a page with a 'download this as csv' link. 我正在使用Jinja 2创建带有“以csv格式下载”链接的页面。
  2. When you click the link, the file is downloaded correctly, starts with the correct content, and at the end it has got the template content added. 当您单击链接时,文件将正确下载,并以正确的内容开头,最后它已添加了模板内容。

Here's my class: 这是我的课:

class xmlAnalyzer(Handler):
    def get(self):
        displaySample = self.request.get('SamplePage')
        downloadMapping = self.request.get('downloadMapping')

        if downloadMapping:
            r = self.request.get('srcTgtMapping')
            srcTgtMapping = r.split('], [')

            csvContents = 'Column name; Source name; Source type; Column name; Target name; Target type;\n'
            self.response.headers['Content-Disposition'] = 'attachment; filename=' + str(downloadMapping) +'.csv'
            self.response.out.write(csvContents)

        elif displaySample == '2':
            testString = 'abracadabra'
            self.response.headers['Content-Disposition'] = 'attachment; filename=' + 'testFile.csv'
            self.response.write(testString)

Now while the second case works fine (if SamplePage=2 parameter is provided) file contains just the word 'abracadabra'. 现在,尽管第二种情况可以正常工作(如果提供了SamplePage=2参数),则文件仅包含单词“ abracadabra”。 In the first case however, the dowloaded file looks as follows: 但是,在第一种情况下,下载的文件如下所示:

Column name; Source name; Source type; Column name; Target name; Target type;
<!DOCTYPE html>

<link rel="stylesheet" type="text/css" href="/stylesheets/styles.css">
<Content-Type: text/html; charset=utf-8>
<html>
<body class="body">

<div class="main-title">Welcome to XML Analyzer for Informatica PowerCenter</div>

The first line is what I expect. 第一行是我所期望的。 But that should be all. 但是,仅此而已。 The rest is the template - any idea why is this getting added? 剩下的就是模板-知道为什么要添加它吗? Thanks in advance! 提前致谢!

Hard to say, but intuitively, deciding by the code you showed us, I would guess it's either... 很难说,但是凭直觉,根据您显示给我们的代码来决定,我想可能是...

  • got to do with the self.response.out.clear() in the first if block—the second one doesn't have it; 与第一个if块中的self.response.out.clear()有关,第二个不包含它;
  • or with automatic template rendering by the webapp2 framework; 或通过webapp2框架自动呈现模板; but this doesn't explain why the 2 cases behave differently, so this is what made me notice that self.response.out.clear() call. 但这并不能解释为什么这两种情况的行为有所不同,所以这让我注意到了self.response.out.clear()调用。

PS by the way, are you sure you've shown us all the relevant pieces of your code? PS ,顺便说一句,您确定您已经向我们展示了代码的所有相关部分吗?

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

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