【发布时间】:2019-09-02 08:00:51
【问题描述】:
我正在尝试从用户上传 csv 文件并将其显示在模板上。我搜索了很多错误,但没有任何帮助。所以我决定向这个伟大的社区寻求帮助。这是我的代码
views.py
@csrf_exempt
def offlineResults(request):
screenNametestList = []
friendsCountList = []
followersCountList = []
favouriteCountList = []
listedCountList = []
statusCountList = []
genEnabledList = []
protectedList = []
verifiedList = []
defaultProfileList = []
botsList = []
if request.method == 'POST' and request.FILES['filecsv']:
csv_file = request.FILES['filecsv']
data_set = csv_file.read().decode('UTF-8')
io_string = io.StringIO(data_set)
next(io_string) # skipping 1st line because 1st line contains header file
for column in csv.reader(io_string, delimiter=','):
screenNametest = column[0]
screenNametestList.append(screenNametest)
friends_countTest = column[1]
friendsCountList.append(friends_countTest)
followers_countTest = column[2]
followersCountList.append(followers_countTest)
favouriteCountTest = column[3]
favouriteCountList.append(favouriteCountTest)
listedCountTest = column[4]
listedCountList.append(listedCountTest)
statusCountTest = column[5]
statusCountList.append(statusCountTest)
geoEnabledTest = column[6]
genEnabledList.append(geoEnabledTest)
protectedTest = column[7]
protectedList.append(protectedTest)
verifiedTest = column[8]
verifiedList.append(verifiedTest)
defaultProfileTest = column[9]
defaultProfileList.append(defaultProfileTest)
botsTest = column[10]
botsList.append(botsTest)
dicCSV = {
'sc': screenNametestList,
'friendCount': friendsCountList,
'followersCount': followersCountList,
'favouriteCount': favouriteCountList,
'listedCount': listedCountList,
'statusCount': statusCountList,
'geoEnabled': genEnabledList,
'protected': protectedList,
'verified': verifiedList,
'defaultProfile': defaultProfileList,
'bots': botsList
}
return JsonResponse(dicCSV)
offline.html
<div class="container">
<h1 class="text-center"><b><u>Offline Results</u></b></h1>
<form class="md-form mt-4" method="post" enctype="multipart/form-data">
<div class="file-field">
<input type="file" name="filecsv" accept=".csv">
<button type="submit" id="load_csv" class="btn btn-default">Submit</button>
</div>
</form>
<div class="table-responsive mt-4">
<table class="table" id="data_table">
<thead>
<tr>
<th scope="col">ScreenName</th>
<th scope="col">FriendCount</th>
<th scope="col">FollowerCount</th>
<th scope="col">FavouriteCount</th>
<th scope="col">listedCount</th>
<th scope="col">statusCount</th>
<th scope="col">geoEnabled</th>
<th scope="col">Protected</th>
<th scope="col">Verified</th>
<th scope="col">DefaultProfile</th>
</tr>
</thead>
</table>
</div>
</div><!--end container-->
ajax 部分
$(document).ready(function () {
$('#load_csv').on('click',function (event) {
event.preventDefault();
$.ajax({
url: {% url 'offlineResults' %},
method: 'POST',
dataType:'json',
{#contentType: false,#}
{#cache: false,#}
{#processData: false,#}
success: function (jsonData)
{
$('#load_csv').val('');
$('#data_table').DataTable({
data : jsonData,
columns: [
{ data: 'sc'},
{ data: 'friendCount'},
{ data: 'followersCount'},
{ data: 'favouriteCount'},
{ data: 'listedCount'},
{ data: 'statusCount'},
{ data: 'geoEnabled'},
{ data: 'protected'},
{ data: 'verified'},
{ data: 'defaultProfile'}
]
});
}
})
});
});
它会产生以下错误
django.utils.datastructures.MultiValueDictKeyError: 'filecsv' [2019 年 4 月 11 日 20:41:48] "POST /offlineResults/ HTTP/1.1" 500 17525
我做错了什么。请帮忙
整个回溯
内部服务器错误:/offlineResults/ Traceback(最近调用 最后):文件 "C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py", 第 77 行,在 getitem 中 list_ = super().getitem(key) KeyError: 'filecsv'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件 "C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", 第 34 行,在内部 response = get_response(request) 文件 "C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", 第 126 行,在 _get_response 中 response = self.process_exception_by_middleware(e, request) 文件 "C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", 第 124 行,在 _get_response response = Wrapped_callback(request, *callback_args, **callback_kwargs) 文件“C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\decorators\csrf.py” , 第 54 行,在 Wrapped_view 中 return view_func(*args, **kwargs) 文件“F:\Final Year Project\FYPDjango\FYPapp\views.py”,第 64 行,offlineResults 如果 request.method == 'POST' 和 request.FILES['filecsv']: 文件 "C:\Users\Mustajab\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\数据结构.py", 第 79 行,在 getitem 中 引发 MultiValueDictKeyError(key) django.utils.datastructures.MultiValueDictKeyError: 'filecsv' [2019 年 4 月 11 日 20:41:48]“POST /offlineResults/HTTP/1.1”500 17525
【问题讨论】:
-
debug 是否会给您提供更详细的错误,可能带有一些行号?
-
向我们展示整个错误跟踪。
-
尝试缩小问题所在。不使用ajax还能用吗?
-
if ... and request.FILES['filecsv']失败。首先你应该使用request.FILES.get('filecsv'),因为它可能不存在。其次,您没有在 ajax 请求中上传文件。在 SO 上搜索如何使用 ajax 上传文件。
标签: django