我正在尝试呈现下表(请参见屏幕截图):
我有以下模板代码:
<table class='table'>
{% for row in task_dashboard_rows %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ row.where_html }}</td>
<td>{{ row.key_html }}</td>
<td>{{ row.value_html }}</td>
</tr>
{% endfor %}
</table>
我创建了一个抽象类,以在视图代码中包含逻辑:
class TaskDashboardRow(object):
def __init__(self, where_html=None, key_html=None, value_html=None):
self.where_html = where_html
self.key_html = key_html
self.value_html = value_html
在我的视图函数中的某处,我有以下内容:
dr = TaskDashboardRow()
dr.where_html = _('Voluum')
dr.set_key_html(is_linking=False, label=_('Create New Lander'))
if self.object.vm_campaign_id:
dr.value_html = format_html(_('<span class="text-success">Not Needed</span>'))
else:
dr.value_html = format_html(
_(
'<ul class="nolist">'
'<li>Name: <b>Campaign:{name}</b></li>'
'<li>URL: <a href="#">{url}</a></li>'
'</ul>'
),
url=absolute(reverse('mediabuying:campaign:money', args=(self.object.pk,))),
name=self.object
)
ret.append(dr)
dr = TaskDashboardRow()
dr.where_html = _('Pangu')
dr.set_key_html(is_linking=False, label=_('Update TA Campaign ID'))
if self.object.ta_campaign_id:
dr.value_html = format_html(_('<span class="text-success">No Need To Update</span>'))
else:
dr.value_html = format_html(
_('<a href="{}">Click here</a> to update ta_campaign_id'),
reverse('mediabuying:campaign:update', args=(self.object.pk,))
)
ret.append(dr)
如您所见,由于以下原因,这非常混乱:
ugettext_lazy
)。 view code
中有controller code
(对分离MVC不利吗?)