Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:18+00:00 2026-05-27T06:27:18+00:00

In a flask app I’m working on, I use U.S. state names as part

  • 0

In a flask app I’m working on, I use U.S. state names as part of the url structure. State names are pulled from a python dictionary that links state abbreviations with their respective proper name, e.g.

state_dict = {"Alabama" : "AL", "Alaska" : "AK",...

This is fine when the state name has no spaces. e.g. http://example.com/Alabama/ However, when the state in question has a space in it, it forms a poor url. e.g. http://example.com/North%20Dakota/

I currently get around this by being careful when I create urls using the state names to use something like state=state.replace(' ', '_') as an argument in url_for(). However, it’s cumbersome and seems crude.

How can I better use state names as part of the url without having to manually modify them each time? Extra points if the solution can also be used to change upper case letters to lowercase.

I’ve considered modifying the state dict to be url friendly e.g. north_dakota instead of North Dakota however, the dict is also used when creating text to display to users, and readability counts.

Thanks very much for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:27:19+00:00Added an answer on May 27, 2026 at 6:27 am

    The most common pattern for python would be to use a pair of dictionaries for the forward / reverse lookup to go from and to your friendly dictionary. On a side note, the term commonly used for such a “url-friendly” representation of a string value is “slug”.

    # state_slugs lets you lookup either state name or code => its slug
    state_slugs = {}
    # states_from_slugs lets you lookup the slug => the state name
    states_from_slugs = {}
    
    for state_name, state_code in state_dict.items():
        slug = state_name.lower().replace(' ', '_')
        state_slugs[state_name] = slug
        state_slugs[state_code] = slug
        states_from_slugs[slug] = state_name
    

    You would put this somewhere it’s run only once, like possibly at module-level, or soon after your state_dict has been created.

    Then accessing either state_slugs['NC'] or state_slugs['North Carolina'] will both work to return “north_carolina” and accessing states_from_slugs['north_carolina'] will return ‘North Carolina’ for the reverse lookup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Python with the tutorial app from Flask and I can't figure how
I'm working on a flask app that needs authentication. I've hooked up flask-login but
I'm intending to create a flash app that pulls images from facebook and displays
My files are as follows: helloworld.py from flask import Flask app = Flask(__name__) @app.route('/')
For example, this code: from flask import session @app.route('/is_logged_in/') def is_logged_in(): return 'user' in
I have a Python Flask app I'm writing, and I'm about to start on
I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is
I am developing web app on flask, python, sqlalchemy and postgresql. My question is
I'm currently working on a project using Flask and Google App Engine . Calling
I am working on a web app based on Flask and SQLAlchemy. Most of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.