views.py
class PartnerListView(ListView):
model = Partner
template_name = 'contacts/partner_list.html'
def get_context_data(self, *args, **kwargs):
context = super(PartnerListView, self).get_context_data(**kwargs)
context['categories'] = Category.objects.all()
if self.kwargs['estil'] == 'lst':
context['line'] = True
elif self.kwargs['estil'] == 'kbn':
context['line'] = False
if self.kwargs['cat'] == 'cat':
context['groupByCat'] = True
else:
context['groupByCat'] = False
return context
class PartnerCreateView(CreateView):
model = Partner
form_class = PartnerForm
template_name = 'contacts/partner_create_form.html'
urls.py
app_name = 'contacts'
urlpatterns = [
url(r'^list/(?P<estil>lst|kbn)/(?P<cat>cat|alf)/$', PartnerListView.as_view(), name='list'),
url(r'^create/$', PartnerCreateView.as_view(), name='create'),
url(r'^(?P<pk>\d+)/$', PartnerDetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/update/$', PartnerUpdateView.as_view(), name='update'),
url(r'^(?P<pk>\d+)/delete/$', PartnerDeleteView.as_view(), name='delete'),
]
template
{% if groupByCat %}
<button type="button" name="" class="btn btn-default" style="margin-left:4px" onclick=location.href="{% url 'contacts:list' estil='lst' cat='cat' %}">
<img src="{% static 'images/icons/icons8-list-24.png' %}" alt="" />
</button>
<button type="button" name="" class="btn btn-default" style="margin-left:4px" onclick=location.href="{% url 'contacts:list' estil='kbn' cat='cat' %}">
<img src="{% static 'images/icons/icons8-grid-24.png' %}" alt="" />
</button>
{% else %}
<button type="button" name="" class="btn btn-default" style="margin-left:4px" onclick=location.href="{% url 'contacts:list' estil='lst' cat='alf' %}">
<img src="{% static 'images/icons/icons8-list-24.png' %}" alt="" />
</button>
<button type="button" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:list' estil='kbn' cat='alf' %}">
<img src="{% static 'images/icons/icons8-grid-24.png' %}" alt="" />
</button>
{% endif %}
{% if line %}
<button type="button" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:list' estil='lst' cat='cat' %}">
<img src="{% static 'images/icons/icons8-user-groups-24.png' %}" alt="" />
</button>
<button type="submit" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:list' estil='lst' cat='alf' %}">
<img src="{% static 'images/icons/icons8-alpha-24.png' %}" alt="" />
</button>
{% else %}
<button type="button" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:list' estil='kbn' cat='cat' %}">
<img src="{% static 'images/icons/icons8-user-groups-24.png' %}" alt="" />
</button>
<button type="submit" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:list' estil='kbn' cat='alf' %}">
<img src="{% static 'images/icons/icons8-alpha-24.png' %}" alt="" />
</button>
{% endif %}
<button type="button" name="" class="btn btn-default" style="margin-left:4px"
onclick=location.href="{% url 'contacts:create' %}">
<img src="{% static 'images/icons/add-new-24.png' %}" alt="" />
</button>
As you can see, the first URL has 2 arguments and the next one does not. The first URL works correctly, but when calling the second URL with a button with {% url 'contacts:create' %}
, Django tries to render the first, not receiving the expected parameters, throws the error:
NoReverseMatch at /contacts/create/
Reverse for 'list' with keyword arguments '{'estil': 'kbn'}' not found. 1 pattern(s) tried: ['contacts/list/(?P<estil>.+)/(?P<cat>.+)/$']
How do I get me to call the correct URL?