May 8, 2017
If you're here for this first time, this is a continuation of a series about the making of a new website and custom CMS for my friend Chris Condon. You can check out his current website at chriscondonsax.com.
Any journey starts with the first step, and developing this website is no exception. After going through this process a couple times I’ve found that a “back to front” approach to developing a website is what works for me. I start with modeling the data, then write the code that will fetch the data, then write the HTML template where the data will ultimately be displayed. I didn’t find this from any specific source, it’s just what works for me.
Until now I’ve been using a database called SQLite for my Django projects. I haven’t used it because it’s best, I’ve used it because it’s the default and is easy to set up. For this project I decided to move to MySQL. MySQL offers more options about how data is accessed, including the ability to have multiple users with different permissions able to access the same database. MySQL is also faster and can handle more data than SQLite. While I doubt Chris will need these for a single-user CMS, it’s something I’ll have to deal with eventually so I might as well learn now.
Unlike SQLite, MySQL didn’t come preinstalled with my Operating System. Getting MySQL up and running took a fair bit of googling. I found Digital Ocean’s tutorial to be the most up-to-date resource. Even this, however, doesn’t account for installing different versions of some things to work with Python 3 instead of Python 2. If you’re using Python 3 (which I am) and following the Digital Ocean tutorial (which I did) on a standard Ubuntu-based Linux distribution (where Python 2 is still default) you’ll need to make a few changes.
sudo apt-get install python-pip python-dev mysql-server libmysqlclient-dev
sudo apt-get install python3-pip python3-dev mysql-server libmysqlclient-dev
This will ensure you get pip and python-dev for Python 3 instead of Python 2.
The above commands install all the packages you’ll need on your system to get started using MySQL with Python3. Most distributions of Linux come with some of these by default.
Setting Up the Database and Virtual Environment
The Digital Ocean tutorial will take you error-free through the database setup. In summary you’ll start a databse for your project, create a MySQL user to interact with your database, and put that user’s information into your Django project’s settings.py file.
Next you’ll need to set up your virtual environment. If you’ve never used one before, a virtual environment is like a sandbox where you can install software packages like you would on your computer. When you’re working in your virtual environment, anything you install with “pip” or “pip3” will be accessible only from within that box. Using a virtual environment also lets you install different versions of packages for different projects.
To set up a virtual environment that uses Python 3, just make the following modifications from the tutorial:
sudo pip install virtualenv
sudo pip3 install virtualenv
Pip3 tells your distro to look for the version of the virtualenv package compatible with Python 3 instead of Python 2.
To start your virtual environment:
virtualenv -p python3 *myprojectenv*
to tell virtualenv explicitly that you want a virtual environment with Python 3 and not Python 2. Because your virtualenv will now only have Python 3, any time you want to install a Python package you can do it via “pip” instead of “pip3.” For example while your virtualenv is active:
pip install django
will install the latest version of django for python 3 in your virtual environment.
Now it’s time to actually write code! Check back soon for The Condon Project: Part 2.