I’m creating a webapplication in Python (and Flask) where a user can register with their wanted username. I would like to show their profile at /user/ and have a directory on the server for each user.
What is the best way to make sure the username is secure for both a url and directory? I read about people using the urlsafe methods in base64, but I would like to have a string that is related to their username for easy recognition.
The generic term for such URL-safe values is “slug”, and the process of generating one is called “slugification”, or “to slugify”. People generally use a regular expression to do so; here is one (sourced from this article on the subject), using only stdlib modules:
The linked article has another 2 alternatives requiring additional modules.