无法使用Python下载文件

时间:2017-09-12 09:19:34

标签: python urllib2

尝试使用Python从远程源下载文件时出错。我在下面解释我的代码。

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 198, in _get_response
    "returned None instead." % (callback.__module__, view_name)
ValueError: The view plant.views.downloadfile didn't return an HttpResponse object. It returned None instead.

我正在解释下面的代码。

status.html:

{% extends 'base.html' %}

{% block content %}
<center>
<form method="post" action="{% url 'downloadfile' %}">
{% csrf_token %}
<label>Type File Name: </label>
<textarea rows="4" cols="100" name="file">
</textarea>
<br>
<button type="submit">Download</button>
</form>
</center>
{% endblock %}

views.py:

 def downloadfile(request):
    """ This function helps to download the file from remote site"""

    if request.method == 'POST':
        URL = request.POST.get('file')
        filename = "status"
        with open(filename,'wb') as fyl:
            fyl.write(urllib2.urlopen(URL).read())
            fyl.close()
        pers = User.objects.get(pk=request.session['id'])
        root = []
        user_name = pers.uname
        count = 1
        root.append(
            {'username': user_name,
             'count': count
                 })
        return render(request, 'plant/home.html', {'user': root, 'count': 1})

在这里,我尝试从http://www.blog.pythonlibrary.org/wpcontent/uploads/2012/06/wxDbViewer.zip等远程源下载文件并获取这些错误。

0 个答案:

没有答案