与Django的Url争论

时间:2016-10-16 12:00:32

标签: python django url

我在Django(1.9)

中遇到了一些问题

尝试了许多方法来解决它,但仍然是同一类型的错误

('a', 1.1)
('a', 1.2)
('a', 1.3)
('b', 2.1)
('b', 2.2)
('b', 2.3)
a
(0, 1, 1.1)
(1, 10, 1.2)
(2, 100, 1.3)
(3, 2, 2.1)
(4, 20, 2.2)
(5, 200, 2.3)
b
(0, 1, 1.1)
(1, 10, 1.2)
(2, 100, 1.3)
(3, 2, 2.1)
(4, 20, 2.2)
(5, 200, 2.3)

实际代码如下:

查看:

Reverse for 'elus' with arguments '()' and keyword arguments '{u'council': u'CFVU'}' not found. 1 pattern(s) tried: ['elus/(?P<council>[A-B]+)$']

url:

class RepresentativeView(ListView):
    model = Representative
    template_name= 'lea/elus.html'
    context_object_name = 'represents'

    def get_queryset(self, council):

        return Representative.objects.filter(active=True).filter(council=council).order_by(order)

模板:

url(r'^elus/(?P<council>[A-B]+)$', views.RepresentativeView.as_view(), name='elus'),

我尝试过{% url 'elus' council='CFVU' %} 等事情。它与**kwargs一起使用url中**kwargs的另一个函数,我的查询基于<pk>。但是在这里,我找不到解决方案。

1 个答案:

答案 0 :(得分:1)

[A-B]只会匹配字母A和B.

如果你只想匹配大写字母,你可以这样做:

url(r'^elus/(?P<council>[A-Z]+)$

或者,常见的方法是使用[\w-]+,它将匹配大写字母A-Z,小写字母a-z,数字0-9,下划线和连字符:

url(r'^elus/(?P<council>[\w-]+)$