format of returned result for .objects.filter(name__containe=data) without quotes sign

120 Views Asked by At

I have written a code by django in which a user can search for a word in a table of database. My problem here is when I print the returned results, they contain quotations. How can I omit them?

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
                  })

The output of this code is like:

 'Creator': 'Davide'

But I just want to have:

Davide

Whats wrong in my effort?


My code for writing the fetched result into a csv file: (Since from the comments I understood this part is related too)

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
1

There are 1 best solutions below

6
Psalms Kalu On

Whenever you run a queryset like data_filed.objects.filter(g__contains=data_g).values() it always returns a dictionary kind of output with keys and values. And when you passed that result to your template file using:

return render(request, 'search_report.html',
                  {
                      'form': form,
                      'data': data,
                      'download_form': download_form
                  })

It is still in that format. If you want to output only the value, you need to output it as such

{{ data.Creator }}

That will give you the output "Davide" without the quotes