0
Hello, I am implementing a button to turn a Query into a spreadsheet and make this download available. For this I created a View to generate Excel, follows:
def export_view_csv(request):
if request == 'POST':
dados = request.POST.get('csv_sender')
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=Consulta' + str(datetime.now()) + '.csv'
writer = csv.writer(response)
writer.writerow(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'])
for info in dados:
writer.writerow(
[info['nomefa'], info['razaos'], info['cnpj'], info['uf'], info['situac'], info['porte'], info['lograd'],
info['numero'],
info['comple'], info['muibge'], info['dsituc'], info['capsoc']])
return response
and created the form by sending to this view the hidden field, follows Forms:
<form method="POST" action = "{% url 'core:csv-export' %}">
<div class="module-option clearfix">
<div class="input-append pull-left">
<input type="hidden" name="csv_sender" value="{{ results }}"/>
{% csrf_token %}
<button type="submit" class="btn" name="submit">
<a>Baixar Planilha</a>
</button>
</div>
</div>
</form>
But I’m facing the following mistake and I don’t know how to proceed:
AttributeError at /csv-export/
'str' object has no attribute 'get'
py.
app_name = 'core'
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('mylogout/', LogoutView.as_view(), name='mylogout'),
path('register/', RegisterView.as_view(), name='register'),
path('contato/', contato_email, name='contato'),
path('csv-export/', export_view_csv, name='csv-export'),
]
If anyone can give me a light...
Puts the
urls.py
in your post as well.– Paulo Marques
Of course, placed!
– Caike Colantonio
Sorry to ask in homeopathic doses, you can put the view that calls the form?
– Paulo Marques
It is the first code of the post as I understood your question
– Caike Colantonio
So, you have no other view related to this process of generating and downloading?
– Paulo Marques
The variable
request
is coming to your view as a string and not as the object containing the HTTP request coming from the browser (so much so that the validationrequest == "POST"
is working because it should berequest.method == "POST"
).– Giovanni Nunes
Thanks Giovanni, this was exactly my problem. I managed to solve thanks for the help
– Caike Colantonio