3 min read

    Validating Form Input With Django

    By Jeff Maxim on Aug 12, 2020 9:26:56 AM

    Let’s say I’m making a blogging web application using Python and the web framework, Django. I want users to be able to publish “Posts” on my blogging website.

    When they enter that “Post” into some sort of input form that I have on my website, I want to check the “Post” before I save it to my database.

    This is a common scenario for many Django developers. How do we validate form data before we save it to a database?

    There’s two common ways to do it: Either validate the Model or validate the Form.

    Django has Forms and Models. Very basically, Forms are where users input data on a web application, and Models and where I actually save my information in my program.

    Let’s look first at how to validate the Model:

    Topics: jeff maxim Python Programming Tips django
    6 min read

    Intro to SQLite3 With Python's Flask

    By Jeff Maxim on Aug 6, 2020 6:55:42 AM

    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.

    Topics: SQL jeff maxim Python Programming Tips

    Featured