alonisser
alonisser

Reputation: 12098

using unicode strings with white space as Django url variable

Is there a problem with using unicode (hebrew specificaly) strings including white space.

some of them also include characters such as "%" .

I'm experiencing some problems and since this is my first Django project I want to rule out this as a problem before going further into debugging.

And if there is a known Django problem with this kind of urls is there a way around it?

I know I can reformat the text to solve some of those problems but since I'm preparing a site that uses raw open government data sets (perfectly legal) I would like to stick to the original format as possible.

thanks for the help

Upvotes: 0

Views: 978

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239440

Django shouldn't have any problems with unicode URLs, or whitespace in URLs for that matter (although you might want to take care to make sure whitespace is urlecoded (%20).

Either way, though, using white space in a URL is just bad form. It's not guaranteed to work unless it's urlencoded, and then that's just one more thing to worry about. Best to make any field that will eventually become part of a URL a SlugField (so spaces aren't allowed to begin with) or run the value through slugify before placing it in the URL:

In template:

http://domain.com/{{ some_string_with_spaces|slugify }}/

Or in python code:

from django.template.defaultfilters import slugify

u'http://domain.com/%s/' % slugify(some_string_with_spaces)

Upvotes: 1

Pep_8_Guardiola
Pep_8_Guardiola

Reputation: 5252

Take a look here for a fairly comprehensive discussion on what makes an invalid (or valid) URL.

Upvotes: 1

Related Questions