0

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

13
  • 2
    The url tag tries to generate valid urls and obviously spaces need to be %20's etc. From what I remember the url tag does this well, so your view needs to adjust this conversion as required, or try passing the data through a post/get request Commented Oct 6, 2015 at 11:17
  • thanks, how i process that in the view properly? Commented Oct 6, 2015 at 11:18
  • replace %20 in the string with spaces etc, not sure if there is a library that does this for you Commented Oct 6, 2015 at 11:26
  • there's no way to get the url back with no %20... etc It also can has accents.... Commented Oct 6, 2015 at 12:08
  • 1
    @Sayse {% url %} accepts positional arguments even when you define named groups in your regex. Commented Oct 6, 2015 at 13:10

1 Answer 1

1

Your regex, [0-9A-Za-z._%+-]+, does not match non-ascii characters. You have to use \w instead. The URL dispatcher will set the re.UNICODE flag for compiled regexes, and this will match any alphanumerical unicode character.

You'd also want to add $ to the end of your regex to match the end of line.

url(r'^dist=(?P<district>[\w\.%+-]+)/$', views.district, name='district'),
Sign up to request clarification or add additional context in comments.

8 Comments

I'm having this error now NoReverseMatch at / Reverse for 'district' with arguments '(u'Districte 5',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['dist=(?P<district>[\\w\\.%+-]+)/$']
That's because your district has a space, but your regex doesn't allow spaces. Either add \s to the regex pattern, or better, use a proper slug.
it now work with spaces but it fails for characters like 'ó', 'à', 'ï'.... as in UnicodeEncodeError at /dist=Sants-Montjuïc/ 'ascii' codec can't encode character u'\xef' in position 29: ordinal not in range(128)
That's a new error. Please post a new question with the full traceback.
isn't it the same error of not accepting not ascii characters in url tag parameters?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.