Being new to Django & Python programming in general, I thought I’d write up some notes on how to get started with a Django web application. I have plans as to where I want to get to with this series – but I’m not going to make any promises yet as I don’t want to make a rod for my own back.
Being a dummy in this area, I do not promise everything I say here is the best/right way to do it. It is just what I’ve discovered. If you think there are better ways to do something, let me know.
Most of the time I’ll assume you’re using a unix-type environment.
Step 0.0 – Python
The first step in working with Python is to ensure you have a reasonably up-to-date version of python installed.
$ python -V Python 2.7.16
Python 2 isn’t good enough nowadays. We want python 3.x
Due to incompatibilities between python versions 2 and 3, you tend to find that both may be installed in your environment, with the command “python” linked to one particular version. If python v3 is installed, it may be available under the name “python3”
$ python3 -V Python 3.7.5
MacOS & Python
If you’re a MacOS user, MacOS comes with python 2 but not version 3 (And Apple have said they’ll remove python altogether from future versions of MacOS). An easy way to install python 3 is to use the Homebrew system:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" $ brew install python
Step 0.1 – Virtualenv
Although python has lots of features out of the box, there is a lot that isn’t in the box. A web application framework isn’t one of them. When installing add-ons to Python, there are two options: The first is to install the add-on as part of the python installation on the machine. The advantage is that the package is available for all uses/users of the machine. The downside, is if you run an application on another machine, you have to remember to install the same packages on the other machine.
So python has the option of creating a virtual environment. This allows you to compartmentalize the requirements for a piece of software to make running it elsewhere much simpler. To do this, you need another package called “virtualenv”
$ which virtualenv /usr/bin/virtualenv
If you get no reponse to the “which” command, you’ll need to install it. It may be available via your package manager. e.g.
$ sudo apt-get install virtualenv
or, you may need to install it via pip:
$ pip3 install virtualenv
Step 1 – Creating a virtual environment
Now we’ve got the core python system installed, we’re ready for our first steps.
First off is to create a virtual environment for our Django application. At a basic level, this is just a directory with some particular files in it.
$ virtualenv -p python3 virtualenv
The “-p python3” is needed if you have both python 2 & 3 installed to ensure your virtual environment gets created to use the correct python version. “Django_Project” is just a directory name.
Once the command has completed, if you look inside the directory, you’ll see three sub directories: bin, include & lib. When we come to commit our Django project to version control, we need to make sure to exclude these.
Now we have a virtual environment, we need to switch it on. Make sure your CWD is Django_Project (Or whatever you named it) and then execute “source bin/activate”
$ source bin/activate (virtualenv) $
Notice the change to your command prompt.
To leave the virtual environment, you can either exit the shell or run the command “deactivate”
(virtualenv) $ deactivate $
Notice the command prompt changes back.
We are now ready to install Django into our virtual environment. So, make sure your virtual environment is active, then issue the command “pip install django”
(virtualenv) $ pip install django
This will download and install the latest Django version into our environment.
Step 2 – Creating a Django Application
We’re now ready to create a blank Django project. Again, make sure you’ve activated your virtual environment:
(virtualenv) $ django-admin startproject project .
Note the space & full-stop at the end of that command.
This will create a folder called “project” in your current directory. You should now see four directories and a file:
... ├── manage.py └── project ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ └── settings.cpython-37.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py
This has created a Django “project” into which we can create “applications”.
(virtualenv) $ django-admin startapp main
You should now see a directory called “main” alongside all the others.
... ├── main │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py ├── manage.py └── project ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ └── settings.cpython-37.pyc ├── asgi.py ├── settings.py ├── urls.py └── wsgi.py
Step 3 – Configuring our Django Application
We’re close now to being able to run our Django application for the first time. Before we run it, we have to do one final step: We have to tell the Django “project” about the “application” that we created.
Edit the file projects/settings.py and look for the block:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
At the end of that list, we need to add in “main” (Or whatever you called your application), so it looks:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'main', ]
Now, we can finally run our Django application with the command:
(virtualenv) $ python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. January 08, 2020 - 19:39:55 Django version 3.0.2, using settings 'project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
If you point your browser at http://127.0.0.1:8000/ you should see the following: