Our new instructor Jeff is also an author, here are some tips from his blog, a great coding resource:
I’ve got a URL shortening website like Bitly that allows you to submit a link, then gives you a new, hopefully shorter url to use for that link. I built it with Python and the Flask microframework.
Right now, a user enters a URL at my flask app, such as “www.jeffreymaxim.com”. My python code will generate a random key for that site, such as “98rDv64O”. It will store both the original URL and the key in a python dictionary like so:
<span class="s1">my_links["www.jeffreymaxim.com"] = "98rDv64O"</span>
So that I’ve got an entry in the my_links dictionary that looks like this:
<span class="s1">{'www.jeffreymaxim.com': '98rDv64O'}</span>
This code allows me to redirect a user from herokuapp to jeffmaxim.com .
The problem here is that this dictionary only lives in the local memory of my machine while my app is running. As soon as I restart my server, that whole dictionary and all my links and keys gets deleted.
The solution is to implement a database.