Django Dictsort by_uuustr__
我有一个项目,其中一个模型是“机箱”。我想在django模板中按照Django Dictsort by_uuustr__,django,templates,Django,Templates,我有一个项目,其中一个模型是“机箱”。我想在django模板中按照\uuuu str\uuuu返回的值对这些机箱进行排序。如果我写dictsort:“\uu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我目前正在编写一个函数getIP,返回与\uu str\uuu相同的内容,并传递| dictsort:“getIP” 有没有一种方法可以做到这一点,而不用简单地在不同的函数中重写完全相同的代码?为什么
\uuuu str\uuuu
返回的值对这些机箱进行排序。如果我写dictsort:“\uu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我目前正在编写一个函数getIP
,返回与\uu str\uuu
相同的内容,并传递| dictsort:“getIP”
有没有一种方法可以做到这一点,而不用简单地在不同的函数中重写完全相同的代码?为什么在模板中按此排序?在传递视图之前在视图中排序-通常尝试在可能的情况下将业务逻辑排除在模板之外,这听起来很像业务逻辑 例如,我的一个模型:
class Event(models.Model):
date = models.DateField()
location_title = models.TextField()
location_code = models.TextField(blank=True, null=True)
picture_url = models.URLField(blank=True, null=True, max_length=250)
event_url = models.SlugField(unique=True, max_length=250)
def __str__(self):
return self.event_url + " " + str(self.date)
def save(self, *args, **kwargs):
self.event_url = slugify(self.location_title+str(self.date))
super(Event, self).save(*args, **kwargs)
考虑到\uuuu str\uuuuu
的输出,这里总是会有一个事件\u url+一些其他的东西(大概是你的),我可以使用以下内容:
stuff_in_order = Event.objects.filter(#yourqueryhere).order_by('event_url').order_by('date')
这与在模板中咀嚼str方法以订购机箱的效果相同。(sp?)
如果确实需要在模板中重新排序,那么可以使用
{% regroup chassis by dealer as dealer_list %}