使用csv和html进行django http响应

时间:2016-02-17 10:30:47

标签: python django csv

我试图找到一种方法来处理django url中的一些数据,并以csv格式返回它,我发现here这样做了。

它有效,但问题是当我得到csv时,我也会收到以下错误:

TypeError at /game/play
__init__() got an unexpected keyword argument 'text'
Request Method: GET
Request URL:    http://127.0.0.1:8000/game/play
Django Version: 1.9.2
Exception Type: TypeError
Exception Value:    
__init__() got an unexpected keyword argument 'text'
Exception Location: /usr/local/lib/python2.7/dist-packages/django/http/response.py in __init__, line 283
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/home/s/djangostuff/mysite',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:    Wed, 17 Feb 2016 10:14:31 +0000

我找不到一种方法来返回包含csv和常规文本和/或一些不错的html页面的响应以及我的csv。

代码在这里:

 def play(request):
    l = ['what','the','h',2123]
    response = HttpResponse(content_type = 'text/csv') 
    response['content-Disposition'] = 'attachment; filename="somthing.csv"'
    writer = csv.writer(response) 
    writer.writerow(l)
    writer.writerow(l)
    return response 

1 个答案:

答案 0 :(得分:0)

我认为您正在阅读第一行中有标题的csv文件,您应该跳过第一行然后阅读