I'm using a custom templatetag
@register.inclusion_tag('parts/sidebar.html', takes_context = True)
def show_sidebar(context):
districts = models.Apartment.objects.order_by().values_list('district', flat=True).distinct()
return {
'districts': districts
}
in the sidebar.html it gets the districts and pass them throug a {% url %} tag with district argument to views.district
sidebar.html
{% load aptAPI_tags %}
<ul>
{% for district in districts %}
<li>
<a href="{% url 'district' district %}">
{{ district }}
</a>
</li>
{% endfor %}
</ul>
views.py
def district(request, district):
try:
apartments = Apartment.objects.filter(district=district).all()
except Apartment.DoesNotExist:
raise Http404("District does not exist")
return render(request, 'district.html', {'apartments': apartments})
urls.py
url(r'^dist=(?P<district>[0-9A-Za-z._%+-]+)', views.district, name='district'),
It works just fine with districts as "Eixample" but it doesn't work for districts with non Ascii character as "Horta-Guinardó" or "Sant Andreu" as the string splits on the first non Ascii character and i need them later to filter on the database. Please any idea? Any help? Thank you
{% url %}accepts positional arguments even when you define named groups in your regex.