Raggi Steinsen
Raggi Steinsen

Reputation: 93

Reverse Django URL with special chars

I'm required to have part number that is defined to any character in addition to widespace, forward slash(/) and dash(-). Furthermore I want to have urls like /part//

I'm trying to get both urls.py to work in addition to reverse url lookup in my templates.

In urls.py I have defined

url(r'^part/(?P<part_number>[A-Z0-9- ]{3,20})', 'inventory.views.part'),

and in my templates I'm using

{% url inventory.views.part part.part_number %}"

But this causes "Caught NoReverseMatch while rendering" exception, with both par numbers with slash and space.

I've seen in the admin interface that the space part numbers are shown correctly (just with space) and the part numbers with slash are shown with "_2F" encoding in the url. Example, part number "1A17OE / JHA7660" is encoded as "/part/1A17OE _2F JHA7660/" in the admin interface.

How is does the admin url accomplish this, and how can I do the same thing?

Thanks in advance, Raggi

Upvotes: 1

Views: 306

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

You should use urlencode to encode whitespaces in the part number.

Upvotes: 1

Related Questions