0

我用 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
4

1 回答 1

0

每当您运行像 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”

于 2020-06-18T13:38:56.360 回答