Support Astronomer

Astronomy for engineers | Engineering for astronomers

How to create a web site using Python

As I promised in my last post, I explain here how to create a web site using Python.

There are many options to create a web site using Python. What I explain below is one of the several possible ways. In order to start creating a web site, a maximum of five programs have to be installed:

  1. Python.
  2. Pip.
  3. MySQL (optional).
  4. Virtualenv (optional, but recommended).
  5. Django CMS.

In case that the computer is not yours, and you do not have the required permissions to install programs in your computer (root permissions), you can:

  • Ask your systems administrator to install Python, pip, virtualenv and, optionally, MySQL. You will need to know the root password for MySQL.
  • In case that you are using a shared hosting, you should first ensure that creating web pages using Python is supported. Nowadays, this is rather common and, in case that it is supported, Python, pip, virtualenv and a database will  already be installed. Even though, it is highly recommended that you start creating your web site locally before installing it in a shared hosting.

1. Python

The first step to create a web site using Python is to ensure that Python is installed. In order to know whether Python is installed, and what version is installed, you only have to write, in the command line:

$> python --version
Python 2.7.5

The $> symbol is used to indicate the text that has to be written in the command line (python --version, in this case). The obtained answer should be the Python version installed, or an error message in case that Python is not installed. In case that Python is not installed, the steps described at the Python web page can be followed to install it.

2. Pip

In order to install all the Python required packages, a package manager is required. The Python package manager is called pip and, to install pip, you only need to follow the instructions at its web page. Obviously, you should check whether pip is installed first, by typing:

$> pip --version
pip 1.5.4 from /Library/Python/2.7/site-packages (python 2.7)

The answer can be largely different, depending on the operating system. The answer above is the typical one in a Mac. In case that pip is not installed, the easiest option to install it, with the proper permissions (as root), would be:

$> curl -O
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1309k  100 1309k    0     0   363k      0  0:00:03  0:00:03 --:--:--  363k

$> python

In case that curl is not installed, you can also use wget -o instead of curl -O.

3. MySQL (optional)

The current web page uses a MySQL database. Although not strictly required, you may also install MySQL. In case that you prefer another database instead of MySQL, you can also use PostgreSQL. For those who have no idea about databases, you can skip this step. In case that MySQL is good enough for you, you can check whether it is installed by typing:

$> mysql --version
mysql  Ver 14.14 Distrib 5.6.16, for osx10.7 (x86_64) using  EditLine wrapper

After installing MySQL, creating the web site database is the only thing that you have to do with this language. As an example, with the proper permissions for the MySQL database (as root):

$> echo "CREATE DATABASE example;" | mysql -u root -p
Enter password:

The web site database (named here example) will be created after introducing the root password. That is all you need to do to use MySQL. However, creating a specific user to use the database is highly recommended. In order to create a specific user, you should type:

$> mysql -u root -p
Enter password:
mysql> USE mysql;
mysql> CREATE USER example-user@localhost IDENTIFIED BY 'my-password';
mysql> GRANT ALL ON example.* TO 'example-user'@'localhost';
mysql> EXIT;

With the commands written above, a user called example-user has been created, who can access the example database, hosted in your computer (localhost), using my-password as password. The MySQL database is now ready to be used. You will see how to use it below.

4. Virtualenv (optional, but recommended)

When developing a web site (or any other project) in Python, it is not recommended to install the new packages in the same place than your operating system, since they could interfere with other programs. Therefore, it is highly recommended to create an (virtual) environment where to install all the packages needed to create the web site. This is useful not only to create web sites in Python, but it could also be used for any other program that you want to install, without interfering with the system libraries. Like in the previous cases, you can check whether virtualenv is installed by typing:

$> virtualenv --version

In case that virtualenv is not installed, the instructions provided in its web page can be followed to install virtualenv. However, in case that you already installed pip, you should be able to install virtualenv, with the proper permissions (as root), by typing:

$> pip install virtualenv

Once virtualenv has been installed, you should create a new (virtual) environment (without the need to be root). To create a new environment, you just need to go where you want to install the new Python packages and type:

$> virtualenv env

All the packages that you want to install will be installed inside the new directory defined above. You can use the name that you want, but I called it env to follow the example in its web page. Once the virtual environment has been defined, it has to be activated by typing:

$> source env/bin/activate

5. Django CMS

There are many libraries to create web pages using Python, but the current one was created using Django CMS. As it can be deduced from its name, Django CMS is based on Django. Django is one of the most used environments to create web pages using Python. Django CMS is a Content Manager System (a CMS) for Django. A CMS provides several tools to edit web pages. Django CMS (since version 3.0) provides an awesome way to edit content: you just have to click on the content to edit it. In addition, to install Django CMS, you just have to go to the virtual environment and type:

$(env)> pip install djangocms-installer

Like in the case of virtualenv, pip has been used to install Django CMS. There is, however, an important difference: the virtual environment has been used, and indicated above with (env). Using the virtual environment has three important advantages:

  • You no longer need any special permissions to install the packages, since everything is installed where you want.
  • You can know what has been installed at any time, since you have installed it. In case that you forget what has been installed, the pip freeze command will tell you all the installed Python packages and their versions.
  • You can be sure that none of the installed packages will interfere with other (Python) programs being used.

In any case, Django CMS has not been fully installed, yet. The command above installs a program that will be the responsible to perform the installation and configuration. In order to install Django CMS, you should type:

$(env)> djangocms -p . my-site

Using the command above, you are saying that you want to create a web site using Django CMS in the current directory (-p .) named my-site (to follow the example in its web page). Several questions will start to appear. The Django CMS installer is evolving rather fast and the questions may be slightly different than the following ones:

Database configuration (in URL format) [default sqlite://localhost/project.db]:

The first question asks for a database to be used. In case that you skipped step 3 above, you can also skip this step. In case that you created a MySQL database, you should type:

Database configuration (in URL format) [default sqlite://localhost/project.db]:

As you can see, the type of database (mysql), the user (example-user), the password (my-password), the server (localhost) and the database (example) have to be defined. Other questions will appear after the first one, but you can use their default values:

django CMS version (choices: 2.4, 3.0, stable, develop) [default stable]:
Django version (choices: 1.4, 1.5, 1.6, stable) [default stable]:
Activate Django I18N / L10N setting (choices: yes, no) [default yes]:
Install and configure reversion support (choices: yes, no) [default yes]:
Languages to enable. Option can be provided multiple times, or as a comma
separated list: en,ca

In the last question above, I am telling that I want to use English and Catalan. Obviously, you can use any of the 70 languages supported by Django CMS. A couple of questions regarding date formats appear next:

Optional default time zone [default America/Chicago]: UTC
Activate Django timezone support (choices: yes, no) [default yes]: no

I am using UTC as timezone and I deactivated Django timezone support in the questions above. As a result, all the times introduced in the web page will be in UTC format. This is the recommended format when you are using MySQL. The reason is that Django needs mysql_tzinfo_to_sql to provide timezones when using MySQL, which is not immediate to activate. Even though, you can activate it, if you want.

The installation continues with some questions that can be skipped by using their default values and finishes by asking a user name, an email address and a password to be used when activating the CMS:

Activate CMS permission management (choices: yes, no) [default yes]:
Use Twitter Bootstrap Theme (choices: yes, no) [default no]:
Load a starting page with examples after installation (choices: yes, no)
[default no]:
Username (leave blank to use 'admin'):
Email address:
Password: admin-password
Password (again): admin-password

These are all the required steps to install Django CMS. You can now activate your new web site. The procedure is different for a shared hosing. However, in case that you installed everything locally, you just need to type:

$(env)> python runserver

Opening your browser and typing the address http://localhost:8000, you should see the default Django CMS web page. In order to start editing content, you just need to type http://localhost:8000/?edit. After introducing your username and password (admin and admin-password, in the current example), you can start using all the Django CMS functionality.

It is important to remark, before finishing, that the procedure described above just explains how to install the required programs to start creating a web site using Python. As you can see, programming in Python has not been needed, yet. The great Django potential is that you can easily incorporate almost any Python application in your web site. In addition, many of the most commonly used web applications (e.g., a blog) have already been created. Therefore, in many cases, you just need to install (with pip) the new applications and adapt them to your style.