Reputation: 125
I've tried to build a table in the view and push it out to the template for that view, because doing it in the template isn't proving to be feasible. I can generate a string that has the correct information in it, but when that template variable is evaluated in the browser, there are extra quotation marks and spaces that seem to keep the table from being correctly produced.
Here is the code from the view:
for unique_activity in uniquelist:
currentrow = '<tr data-activityID="' + str(unique_activity[0])+ '">' + '<td>' + str(unique_activity[1]) + '</td>'
for visit in visits_list:
for activity in visit.activity_set.all():
if activity.id == unique_activity[0]:
currentrow = currentrow + '<td>' + 'Reps:'+ str(activity.Repetitions) + '</td>'
else:
noact = True
if noact == True:
currentrow = currentrow + '<td></td>'
currentrow = currentrow + '</tr>\n'
tablerows.append(currentrow)
table = '<table>'
for row in tablerows:
table = table + row
table = table + '</table>'
table = str(table)
The output is what it needs to be.. an example <table><tr data-activityID="1"><td>Initial Evaluation</td><td>Reps:None</td><td></td><td></td></tr> <tr data-activityID="3"><td>Cold Pack</td><td></td><td>Reps:None</td><td></td></tr> <tr data-activityID="6"><td>Recumbent Exercise Bike</td><td>Reps:12</td><td></td><td></td></tr> <tr data-activityID="7"><td>Leg Press</td><td></td><td></td></tr> <tr data-activityID="8"><td>Shoulder Ladder</td><td></td><td></td></tr> </table>
However, this is what shows up in the DOM, all that I output to the template is simply {{ table }} and this is what gets output into the DOM info,
and results in just the string being displayed, not a table.
Here is the template snippet
<body>
{% if activity_list %}
<table>
{% for activity in activity_list %}
<tr>
<td>{{ activity.ActivityType.Name }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{{ table }}
</body>
I have no idea what is up...
Upvotes: 2
Views: 1800
Reputation: 1730
This sounds like you may have autoescape on in your base template (or elsewhere). If you want your variable to be included as HTML and not safely escaped into text, you'll have to either turn autoescape
off around it, or mark it with the safe template filter. Here's an example template to try and illustrate.
<p>The following table should be rendered as escaped safe text.</p>
{{ table }}
<p>But with the safe filter, it will be rendered as html!</p>
{{ table|safe }}
<p>And maybe, I want to do this to a few things or have a block of safe html, so I'll just turn the autoescape off for a bit!</p>
{% autoescape off %}
{{ table }}
{{ some_other_safe_html }}
{% endautoescape %}
Using the snippet you provided, here's the code with the safe escape:
<body>
{% if activity_list %}
<table>
{% for activity in activity_list %}
<tr>
<td>{{ activity.ActivityType.Name }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
{{ table|safe }}
</body>
Upvotes: 2