为什么我在提交html表单时会收到DeprecationWarning?

时间:2014-08-30 16:45:03

标签: python django forms

我有一个html表单,它将文本区域值发送到django视图。然后视图将此数据传递给google-drive api。我在终端上收到以下消息

C:\Python27\Lib\wsgiref\handlers.py:127: DeprecationWarning:创建流媒体 不推荐使用HttpResponse的回复。请改用StreamingHttpResponse  如果你需要流媒体行为。   对于self.result中的数据:

之前我在django视图中使用了HttpResponse,但即使我将其更改为StreamingResponseObject,我也收到相同的警告。

从日志中可以看出,表单提交直到django视图并且请求在上面的消息之前死亡。 我的观点如下:

def main(request):
    if request.POST and request.POST['form'] == 'Save':
        return save(request)

def save(request):
    if request.POST:
        try:
            user = request.user
            social = user.social_auth.get(provider='google-oauth2')
            code = request.POST['hiddentextarea']
            filename = request.POST['filename']
            with open(filename, 'w') as f:
                f.write(code)
            storage = Storage(CredentialsModel, 'id', user, 'credential')
            credentials = storage.get()
            http = httplib2.Http()
            http = credentials.authorize(http)
            service = build('drive', 'v2', http=http)
            resource = createOrUpdateFile(service, filename)
            t = loader.get_template('main.html')
            c = RequestContext(request,{
                    'input' : code,
                    'fileid' : resource['title']
                })
            return StreamingHttpResponse(t.render(c))
        except (AccessTokenRefreshError, Exception):
            return HttpResponseRedirect('../')

我的表格如下:

<form action="/main/" method="post" onsubmit="return copyeditorcontent()">
{% csrf_token %} 
<div id='codeinput' >
<div id='inputlabel'>
    <text>Code</text>
</div>
<div id="editor" name='editor'>
</div>

<div id='buttons'>
  <button id='runbutton' type="submit" value="Run" name="form">Run</button>
  <button id='savebutton' type="submit" value="Save" name="form" >Save to Google Drive</button>
</div>
<textarea id='hiddentextarea' name='hiddentextarea' style='opacity:0;'></textarea>
<div id='filenamesection' style="">
<input id='filename' name='filename' type='hidden' value='{% if fileid %}{{ fileid  }}{% endif %}'></input>
</div>

<script src="../static/src/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/github");
    editor.getSession().setMode("ace/mode/python");
    {% if input %}
    editor.setValue('{{ input|escapejs }}');
    var totalLines = editor.session.getLength();
    editor.gotoLine(totalLines);
    {% endif %}
    var filename = document.getElementById('filename').value;

    if (filename != ''){
        document.getElementById('savebutton').innerHTML = 'Save';
        }
    else{
        document.getElementById('savebutton').innerHTML = 'Save to Google Drive';
        }
</script>

</div>
</form>

仅当我按&#39;保存到Google云端硬盘按钮时,才会收到此警告。请帮帮我。

0 个答案:

没有答案