2024-04-20 10:33:04 发布
网友
我的代码当前列出了domains中的所有server,使用:
domains
server
{% for domain in server.domain_set.all %}
我想按url对视图中的域进行排序。比如:
{% for domain in server.domain_set.all().order_by('url') %}
但我得到一个例外"could not parse the remainder"。我怎样才能订单子?你知道吗
"could not parse the remainder"
“couldnotparseremains”错误是因为您在django模板中包含Python代码。Django不允许这样。你知道吗
可以在模型上添加方法:
def sorted_domains(self): return self.domain_set.all().order_by('url')
然后这样称呼:
{% for domain in server.sorted_domains %}
另一种方法是使用Meta attribute在Domain模型上设置默认排序顺序。你知道吗
Domain
您可以使用dictsort筛选器:
Takes a list of dictionaries and returns that list sorted by the key given in the argument.
{% for domain in server.domain_set.all|dictsort:'url' %}
另请参见:
“couldnotparseremains”错误是因为您在django模板中包含Python代码。Django不允许这样。你知道吗
可以在模型上添加方法:
然后这样称呼:
另一种方法是使用Meta attribute在
Domain
模型上设置默认排序顺序。你知道吗您可以使用dictsort筛选器:
另请参见:
相关问题 更多 >
编程相关推荐