I have a generic CBV that allows users to upload a CSV file. The GET function of the CBV just displays the form for the upload. The POST function takes the CSV, parses it into useable data. After the CSV has been parsed, I want a page rendered that displays the data in an HTML list.
Here is my generic CBV for uploading and parsing
class timeEntry(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
return render(request, "timeEntryFile.html")
def post(self, request, *args, **kwargs):
context = {}
csv_file_input = request.FILES["file_input"]
rows = TextIOWrapper(csv_file_input, encoding="utf-8", newline="")
for row in DictReader(rows):
context_name = row['Textbox_1']
start_date_formatted = datetime.strptime(row['PeriodFrom'], '%m/%d/%Y')
end_date_formatted = datetime.strptime(row['PeriodTo'], '%m/%d/%Y')
time_period_first_day = start_date_formatted - timedelta(days=start_date_formatted.weekday() % 7)
journal_name = row['Textbox_60']
time_period = TimePeriod.objects.get(start_date=start_date_formatted, end_date=end_date_formatted)
date = time_period_first_day + timedelta(days=int(row['Textbox_11']))
approval_status = row['Textbox_32']
customer = row['Textbox_42']
project = row['Textbox_59']
activity = row['Textbox_34'] + ' - ' + row['ActivityTxt'].split(' - ', 1)[1]
line_property = 'Billable' if row['LinePropertyName'] == 'Billable' else 'Non billable'
time_hour = float(row['Textbox_86'])
context[context_name] = {'d365_journal_name': journal_name, 'time_period': time_period, 'date': date, 'approval_status': approval_status, 'customer': customer, 'project': project, 'activity': activity, 'line_property': line_property, 'time_hour': time_hour}
return render(request, "timeEntryReview.html", context)
However, every time, the timeEntryReview.html template does not render the context.
Here is my html template:
{% extends 'base.html' %}
{% block nav_item_time_entry %}bg-indigo-500{% endblock nav_item_time_entry %}
{% load static %}
{% block content %}
<section>
{% if messages %}
<ul class="max-w-3xl mx-5 mb-4 px-9">
{% for message in messages %}
<li class="text-center py-2 px-5">{{ message|safe }}</li>
{% endfor %}
</ul>
{% endif %}
</section>
<main class="flex-1 overflow-x-hidden overflow-y-auto">
<div class="container mx-auto px-6 py-8">
<div class="flex flex-col mt-8">
<div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">
<div
class="align-middle inline-block min-w-full shadow overflow-hidden sm:rounded-lg border-b border-gray-200">
<table class="min-w-full">
<thead>
<tr>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Journal Num</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Time Period</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Date</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Approval Status</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Customer</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Project</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Activity</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Property</th>
<th
class="px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">
Time</th>
</tr>
</thead>
<tbody class="bg-white">
{% for line in context %}
<tr>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-200">
<div class="flex items-center">
<div class="ml-4">
<div class="text-sm leading-5 font-medium text-gray-900">12</div>
</div>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</main>
{% endblock content %}
(Note, right now I am just trying to get anything to show in the template so I do not have the entire table built out.)
Is it possible to pass the context from the render of the post function to the template or not?
Much thanks