如何在Django中替换没有选择字符串的用户名 [英] how do I replace no username with a string of choice in Django
问题描述
当没有用户的票时,我想要没有用户分配。
I would like to have 'No user assigned' when there is no user to the ticket.
我这样做到了我的模型,但我没有看到任何更改。
I did this to my model but I don't see any changes
def __unicode__(self):
print 'gets here****'
if not self.assigned:
return 'No user assigned'
else:
return self.assigned
分配的是分配给机票的用户。
where assigned is the user assigned for a ticket.
有什么其他的方法吗?
IS ther any other way to do this?
我有我的模板和视图:
{% for ticket in tickets %}
<tr>
<td>{{ ticket.title }}</td>
<td>
{% for user in ticket.assigned.all %}
{{ user.email }}{% if not forloop.last %},{% endif %}
{% endfor %}
</td>
和我的查看:
def get_data(self, **kwargs):
context = super(View, self).get_data(**kwargs)
project = self.project_described()
context.update({
"project": project,
"tickets": project.tickets.all()
})
return context
推荐答案
您可以使用 for ... empty
You can use for...empty
将它包含在一个模型中没有什么意义,所以如果在for循环中迭代的东西是空的,那么你可以提供一个默认值
It doesn't make much sense to include it in a model so you can just provide a default if the thing being iterated over in the for loop is empty
{% for user in ticket.assigned.all %}
{{ user.email }}{% if not forloop.last %},{% endif %}
{% empty %}
No user assigned
{% endfor %}
这里的优点是你c总是可以使用空的
部分在表格中插入一个按钮,以允许人们被分配(例如)。
The advantage here is you could always use the empty
section to insert a button into your table to allow people to be assigned (for example).
这篇关于如何在Django中替换没有选择字符串的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!