multiple kwargs in django's template

1

I have a regular expression defined to capture optional parameters in a view.

 url(r'^lista/((?P<tematica>[0-9]+)/(?P<pageIndex>[0-9]+)/(?P<pageSize>[0-9]+))|/$', views.PinturasListView.as_view(), name='lista')

in theory, pageIndex and pageSize are optional (the 3 parameters or none should reach the view). The problem that when I try to put the optional arguments from the template I mark error in the following line:

<a href="{% url 'pintura:lista' tematica='1' pageIndex='0' pageSize='6' %}"> 

however if I use it like this it works:

<a href="{% url 'pintura:lista' %}">

But I need to pass on the kwargs to do what I need. Specifically the description of the error is:

Reverse for 'lista' with arguments '()' and keyword arguments '{u'tematica': u'1', u'pageIndex': u'0', u'pageSize': u'6'}' not found. 1 pattern(s) tried: [u'pinturas/lista/((?P<tematica>[0-9]+)/(?P<pageIndex>[0-9]+)/(?P<pageSize>[0-9]+))|/$']
    
asked by user3155832 23.02.2017 в 02:41
source

1 answer

1

Well, I would recommend you change the end of your url first by something like this:

r'^lista/((?P<tematica>[0-9]+)/(?P<pageIndex>[0-9]+)/(?P<pageSize>[0-9]+)/)?$'

What I did was to change the | (pipe) to a ? , so it is more understandable, then, to solve the error, we must do the following, starting from the fact that you are enclosing in a parenthesis to group, django , the first group to capture will be the parenthesis in general, so you can not solve anything, so to avoid capturing that parenthesis, you add a ?: to omit the group, leaving your url:

r'^lista/(?:(?P<tematica>[0-9]+)/(?P<pageIndex>[0-9]+)/(?P<pageSize>[0-9]+)/)?$'

This way you already ensure that you make the match over what is inside the group, and not the group.

Any questions, comments.

    
answered by 24.02.2017 / 00:11
source