// Я загружаю файл CSV со страницы. Где я извлекаю входные параметры, которые всегда возвращаются None для всех полей, даже если они имеют значение. Перед загрузкой я открываю страницу и ищу параметры с помощью kitty_view (запрос)
// Html Страница {% extends 'base. html'%} {% load stati c%}
{% block content %}
<form class="form-signin" action="{% url 'kitty_view' %}" method="get">
{% csrf_token %}
<div class="container">
<div class="customer">
<div class="form-row mb-3">
<td><a href="{% url 'kitty' %}" class="btn btn-primary btn-sm" role="button">Add Kitty</a> </td>
<td><a style="margin-top:5px;margin-left:20px" href="{% url 'kitty_download' %}" ">CSV Download</a> </td>
<!-- <td><a href=" {% url 'kitty_download' %}" class="btn btn-primary btn-sm" role="button">CSV
Download</a> </td> -->
</div>
</div>
<div class="form-row">
<div class="mb-3">
<select class="custom-select-sm center-block" name="code1" id="code1">
<!-- <option value="{{ code }}">Choose Kitty...</option> -->
<option>{{ code }}</option>
{% for j in kitty_code %}
<option value="{{ j.code }}"> {{ j.code|add:' - '|add:j.name }} </option>
{% endfor %}
</select>
</div>
<div class="mb-3 ml-2">
<input type="text" value="{{ name }}" name="nam" id="nam" class="form-control-sm center-block"
placeholder="Name" autofocus>
</div>
<div class="mb-3 ml-2">
<select class="custom-select-sm center-block" name="stat" id="stat" placeholder="Status">
<!-- <option value="{{ status }}">Choose Status...</option> -->
<option>{{ status }}</option>
<option>A</option>
<option>I</option>
</select>
</div>
<div class="mb-3 ml-2">
<!-- <a href="{% url 'kitty_view' %}" class="btn btn-primary btn-sm" role = "button">Search</a> -->
<button type="submit" class="btn btn-primary btn-sm " role="button">Search</button>
</div>
</div>
</form>
</div>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Kitty Code</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Start Date</th>
<th scope="col">Total Months</th>
<th scope="col">End Date</th>
<th scope="col">Total Members</th>
<th scope="col">Amount</th>
<th scope="col">Installment</th>
<th scope="col">Status</th>
<th scope="col">Details</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
{% if kitty %}
{% for i in kitty %}
<tbody>
<tr>
<td>{{ forloop.counter }} </td>
<td>{{ i.code }} </td>
<td>{{ i.name }} </td>
<td>{{ i.type }} </td>
<td>{{ i.startdate }} </td>
<td>{{ i.durinmonths }} </td>
<td>{{ i.enddate }} </td>
<td>{{ i.totalmembers }} </td>
<td>{{ i.totalamount }} </td>
<td>{{ i.noofinstallments }} </td>
<td>{% if i.status == 'A' %}
{{ 'Active' }}
{% else %}
{{ 'Inactive' }}
{% endif %}
<td><a href="{% url 'kitty_details' i.id %}" class="btn btn-primary btn-sm" role="button">Details</a> </td>
<td><a href="{% url 'kitty_edit' i.id %}" class="btn btn-primary btn-sm" role="button">Edit</a></td>
{% if i.status == 'A' %}
<td><a href="{% url 'kitty_delete' i.id %}" class="btn btn-warning btn-sm confirm-delete" role="button"
onclick="return confirm('Do you want to delete this Kitty?')">Delete</a> </td>
{% else %}
<td><a href="{% url 'kitty_activate' i.id %}" class="btn btn-warning btn-sm confirm-delete" role="button"
onclick="return confirm('Do you want to Activate this Kitty?')">Activate</a> </td>
{% endif %}
</td>
</tr>
</tbody>
{% endfor %}
{% endif %}
</table>
{% endblock %}
// Путь к URL ('kitty_view', views.kitty_view, name = 'kitty_view'), путь ('kitty_download', views.kitty_download, name = 'kitty_download')
// Просмотров
def kitty_download(request):
print(request.method)
url_name = request.resolver_match.url_name
print(url_name)
code1 = str(request.GET.get('code1'))
name1 = str(request.GET.get('nam'))
status1 = str(request.GET.get('stat'))
print(code1)
kittys = kitty.objects.all().order_by('cretime')
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] ='attachment; filename="kitty.csv"'
writer = csv.writer(response, delimiter = ',')
writer.writerow(['Kitty Details'])
writer.writerow(['Code',code1,'Name',name1,'Status',status1])
writer.writerow([])
writer.writerow(['Code','Name','Type','Start Date','Total Months','End date','Total Members','Total Amount','Total Installments','Installment Amount','Status'])
for i in kittys:
writer.writerow([i.code,i.name,i.type,i.startdate,i.durinmonths,i.enddate,i.totalmembers,i.totalamount,i.noofinstallments,i.installmentamount,i.status])
return response
// Before downlowading the CSV file i am populating the page with below view
def kitty_view(request):
url_name = request.resolver_match.url_name
print(url_name)
kitty_list = kitty.objects.all().order_by('cretime')
code1 = str(request.GET.get('code1'))
name1 = str(request.GET.get('nam'))
status1 = str(request.GET.get('stat'))
print(code1)
if (code1 is not None and code1 != 'None' and code1 != ''):
kitty_list = kitty_list.filter(code=code1)
print(1)
if (name1 is not None and name1 != 'None'and name1 != ''):
kitty_list = kitty_list.filter(name=name1)
if (status1 is not None and status1 != 'None' and status1 != ''):
kitty_list = kitty_list.filter(status=status1)
kittys = kitty.objects.all()
ctx = {'kitty': kitty_list, 'kitty_code': kittys,'code':code1,'name':name1,'status':status1}
return render(request, 'kitty/kitty_view.html', ctx)