我正在使用 django 和plotly 开发一个项目。它从 Excel 工作表中读取并相应地显示数据。一切正常,除了我有一个儿童调查列,其中仅填充了一些家长调查。因此,没有子调查的单元格显示“nan”,而不仅仅是空的。这是我在views.py中的函数:
def survey_status_view(request):
# Fetch data for all CLINs and calculate necessary values
clin_data = []
# Get all unique combinations of CLIN and child survey
unique_clins = SurveyData.objects.values('clin', 'survey_child').distinct()
for clin in unique_clins:
clin_id = clin['clin']
child_survey = clin['survey_child']
# Check if the child_survey is 'None' (the string), and handle it appropriately
if child_survey == 'None':
child_survey = '' # Replace 'None' with an empty string
# Filter by both clin and child_survey
survey_data = SurveyData.objects.filter(clin=clin_id, survey_child=child_survey)
# Total units for the current CLIN and child survey
total_units = survey_data.first().units if survey_data.exists() else 0
# Count redeemed units based on non-null redemption dates
redeemed_count = survey_data.filter(redemption_date__isnull=False).count()
# Calculate funds for this CLIN and child survey
total_funds = total_units * survey_data.first().denomination if total_units > 0 else 0
spent_funds = redeemed_count * survey_data.first().denomination if redeemed_count > 0 else 0
remaining_funds = total_funds - spent_funds
# Append results for this CLIN and child survey
clin_data.append({
'clin': clin_id,
'survey': survey_data.first().survey if survey_data.exists() else '',
'child_survey': child_survey, # Use the updated child_survey with empty string if it's 'None'
'status': survey_data.first().status if survey_data.exists() else '',
'launch_date': survey_data.first().launch_date if survey_data.exists() else '',
'expiration_date': survey_data.first().expiration_date if survey_data.exists() else '',
'total_units': total_units,
'redeemed_count': redeemed_count,
'spent_funds': spent_funds,
'total_funds': total_funds,
'remaining_funds': remaining_funds,
'redemption_rate': (redeemed_count / total_units * 100) if total_units > 0 else 0,
})
# Log the results for debugging
for data in clin_data:
print(f"CLIN: {data['clin']}, Child Survey: {data['child_survey']}, Total Units: {data['total_units']}, "
f"Redeemed Count: {data['redeemed_count']}, Spent Funds: {data['spent_funds']}, "
f"Remaining Funds: {data['remaining_funds']}")
# Render the data in the template
return render(request, 'surveyStatus.html', {'clin_data': clin_data})
这是相关的html,但我不认为这就是问题所在:
{% load humanize %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Survey Status</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #002060;
color: white;
}
.active-row {
background-color: #e2f3d7;
}
.inactive-row {
background-color: #fde9d9;
}
</style>
</head>
<body>
<h1>Survey Status</h1>
<form method="GET" action="{% url 'virtual_delivery' %}">
<table>
<thead>
<tr>
<th>SELECT</th>
<th>CLIN</th>
<th>Survey</th>
<th>Child Survey</th>
<th>Status</th>
<th>Launch Date</th>
<th>Expiration Date</th>
<th>Redemption Rate</th>
<th>Total Funds</th>
<th>Spent Funds</th>
<th>Remaining Funds</th>
</tr>
</thead>
<tbody>
{% for clin in clin_data %}
<tr class="{% if clin.status == 'Active' %}active-row{% elif clin.status == 'Pre-Launch' or clin.status == 'Inactive' %}inactive-row{% endif %}">
<td><input type="checkbox" name="clin" value="{{ clin.clin }}"></td>
<td>{{ clin.clin }}</td>
<td>{{ clin.survey }}</td>
<td>{{ clin.child_survey }}</td>
<td>{{ clin.status }}</td>
<td>{{ clin.launch_date }}</td>
<td>{{ clin.expiration_date }}</td>
<td>{{ clin.redemption_rate|floatformat:1}}%</td>
<td>${{ clin.total_funds|floatformat:0|intcomma }}</td> <!-- Show total funds with thousand separator -->
<td>${{ clin.spent_funds|floatformat:0|intcomma }}</td> <!-- Show spent funds with thousand separator -->
<td>${{ clin.remaining_funds|floatformat:0|intcomma }}</td> <!-- Show remaining funds with thousand separator -->
</tr>
{% endfor %}
</tbody>
</table>
<br>
<button type="submit">View Charts</button>
</form>
</body>
</html>
我尝试过 chatgpt,这是我的下一个来源。请有人帮助兄弟。
你能在 html 中尝试一下吗?
<td>{{ clin.child_survey|default_if_none:"" }}</td>
或者你可以在视图上尝试这个吗?
unique_clins = SurveyData.objects.annotate(
child_survey_filled=Coalesce('survey_child', Value(''))
).values('clin', 'child_survey_filled').distinct()