In the jar application I'm working on, I use US state names as part of the URL structure. State names are pulled from a python dictionary that associates state abbreviations with their corresponding proper name, for example.
state_dict = {"Alabama" : "AL", "Alaska" : "AK",...
This is normal when the state name has no spaces. for example http://example.com/Alabama/
However, when the state in question has a space in it, it forms a bad URL. e.g. http://example.com/North%20Dakota/
I currently circumvented this, being careful when I create URLs using state names to use something like state=state.replace(' ', '_')
as an argument in url_for()
. However, it is bulky and seems rude.
What is the best way to use state names as part of a URL without having to manually change them each time? Extra points if this solution can also be used to change lowercase letters. p>
I thought about modifying the state dict to be URL friendly, for example. north_dakota
instead of North Dakota
, however, dict is also used when creating text to display to users and count readability.
Thanks so much for your time!
source share