我用django编写了一个代码,用户可以在其中搜索数据库表中的单词。我的问题是当我打印返回的结果时,它们包含引号。我怎么能省略它们?
def My_data_filed(request):
form = data_filed_Form(request.POST or None)
data = None
download_form = None
if request.method == 'POST':
if form.is_valid():
data_g = form.cleaned_data.get('g')
data = list(data_filed.objects.filter(g__contains=data_g).values())
print(data_filed.objects.all().values())
download_form = data_filed_DownloadForm(initial={
'g': data_g,
})
return render(request, 'search_report.html',
{
'form': form,
'data': data,
'download_form': download_form
})
此代码的输出如下所示:
'Creator': 'Davide'
但我只想拥有:
Davide
我的努力出了什么问题?
我用于将获取的结果写入csv文件的代码:(因为从评论中我了解到这部分也是相关的(
def resultDownload(request):
try:
assert request.method == 'POST'
form = data_DownloadForm(request.POST)
assert form.is_valid()
data_g = form.cleaned_data.get('g')
data = list(data_filed.objects.filter(data__contains=g).values_list())
except AssertionError:
error = 'Your request has some problems.'
data = error
attachment = 'SearchedReport.csv'
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename="{}"'.format(attachment)
response.write(data)
return response
每当你运行像data_filed.objects.filter(g__contains=data_g(.values((这样的查询集时,它总是返回带有键和值的字典输出。当您使用该结果传递到模板文件时,请使用:
return render(request, 'search_report.html',
{
'form': form,
'data': data,
'download_form': download_form
})
它仍然是这种格式。如果只想输出值,则需要按此方式输出
{{ data.Creator }}
这将为您提供不带引号的输出"Davide">