Your Essential Django Setup Checklist: From Virtualenv to Runserver
Welcome to the world of Django web development! Whether you’re a newcomer or a seasoned developer, setting up your Django environment is the crucial first step in creating powerful web applications. In this article, we’ll guide you through the essential checklist to get your Django project up and running smoothly. From creating a virtual environment to starting your Django project and firing up the development server, we’ll cover each step concisely, ensuring you have a solid foundation to kick-start your Django journey. Let’s dive in and get your Django setup from VirtualEnv to runserver!
Table of Contents:
- Install Python and Pip
- Installing and setting up Virtualenv
- Running StartProject and StartApp
- Running Migrations and Configuring Admin
1. Installing Python and Pip
Django being a Python library, we will need to have Python available on our machine. Python is installed by default on many machines, and to check if it is on your, you can either of the following commands in your terminal.
python --version
python3 --version
If python is installed, you will see your version number printed.
If Python is not installed, then you will need to install it manually. You can find specific instructions at python.org.
Similarly, you will want to check if you have python’s package installer Pip installed on your machine. You can do so with either of the following commands.
pip --version
pip3 --version
If pip is properly installed on your machine, you should see the version number printed.
If Pip is not installed, then you will need to install it manually. You can find specific instructions at pip.pypa.io.
Whichever package name is being used on your machine (python/python3 and pip/pip3), you will need to use for any relevant commands for the rest of this guide.
At this point you should have your Python programming language and package manager all set up and ready to go!
2. Installing and setting up VirtualEnv
In order to isolate our packages and keep independent environments for different projects, we will make use of the VirtualEnv package for python. This will allow you to create standalone Python environments to install packages on which will not contaminate your general Python environment or those of your other projects.
Fortunately, with Pip installed, the installation of VirtualEnv is a breeze. Simply run the following commands.
pip3 install virtualenv
virtualenv --help
If successful, you should see something similar to the following output
With VirtualEnv succesfully installed, we can create a folder for our project and initialize an environment with the following commands (on linux).
mkdir djangoStarterProject
cd djangoStarterProject
virtualenv env
On success, an environment folder will be created and you should see the following.
You now have access to an isolated environment which can install packages and be used to run your project. For a standard Django project, we will need to activate the environment and then install Django itself. It is important that you activate the environment before installing your packages, otherwise they will be installed to your machine generally and all advantages of an environment are lost. Activate your environment by running the following in the directory containing your env folder.
source ./env/bin/activate
You will see the name of the environment (in ouse case, env) at the start of your terminal lines upon successful activation. Now, we can install Django with the following.
pip3 install django
Now we have an environment configured to run a Django project. It is important to note that any time you close your terminal or shut down your computer, you will need to reactivate your environment before any further development and testing.
3. Running Startproject and StartApp
With a properly configured environment, we can let Django do the legwork of created the proper directory structure and configuration. We do this using the following command.
django-admin startproject mysitename
cd mysitename
This will fill out a project directory with the proper folders and files for a basic Django project and move you into the folder that contains your, very important, manage.py file. We can make use of this manage.py file to initialize an app within our project.
In Django, an “app” serves as a modular component responsible for handling specific functionalities within a web project. Apps play a crucial role in Django development by promoting code organization, reusability, and maintainability. By breaking down the project into discrete apps, developers can manage different aspects of the application independently, facilitating easier testing, debugging, and scalability.
We can create an app within out project using the following command while within your project folder.
python3 manage.py startapp myappname
Any time we create an app, if we want to use the app within our project, we will need to adjust our settings as well. Head into your site’s folder which will contain your settings.py file and open it with your favorite text / code editor.
With this file open, we want to adjust the INSTALLED_APPS list to include our app by name. Adjust it as follows.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myappname'
]
With that we have an app added to our project which we can use for development of our site! We can now run the built in development server using our manage.py file. To do so, run the following command (in your outer project folder which contains the manage.py file) and open up the url in your browser.
python3 manage.py runserver
Success! However, you will notice the warning when we run the server about “unapplied migration(s)…” which we will address in the next section.
4. Running Migrations and Configuring Admin
At this point you have a functioning application and are able to run the development server, however we have not set up any database connection. This is why we get the warning about unapplied migrations.
In Django, interactions with a given database are done by using the built in Object Relational Mapper (ORM). This means that we will not have to write any SQL code directly, though there are lower level APIs for that, and any commands we write into the ORM will work for any database that Django is configured to work with. Instead of working with SQL tables, we will work directly with models in Django. You can learn more at djangoproject.com.
For our purposes, we need to be able to run makemigrations and migrate through the manage.py file. Makemigrations will generate migration files based on changes made to your models, reflecting alterations in the database schema. These migration files capture the modifications you’ve made to your models, such as adding new fields or altering existing ones.
Migrate will apply these migrations to your database, effectively updating its schema to match the changes in your models. This process ensures consistency between your Django project’s data structure and the underlying database, facilitating seamless management and deployment of updates.
Run the following code to make proper migrations and migrate, so that our server can connect to a backend (SQLite .db files by default).
python3 manage.py makemigrations
python3 manage.py migrate
Now we can once again run the server, but this time without any warning message!
python3 manage.py runserver
With that, we have a Django project properly configured to connect to a database. To see this in action, lets create a superuser, which will be stored in our database, run the server again, and login to the admin console. To do so, run the following code.
python3 manage.py createsuperuser
Fill out credentials for your super user and navigate to the admin console at http://127.0.0.1:8000/admin.
Once here, you can login with the credentials created earlier. If the database is connected properly, you will be logged in and given full access to the admin panel.
Congratulations on setting up your initial Django web application! With this basis you can build your site out to suit your needs using the powerful tools available via the Django web framework. Don’t forget to activate your environment whenever developing and testing so that you can run your python based tools.
The full resulting code for this can be found at https://github.com/taylor-berukoff/django-starter.
Leave a comment if you have any questions and take care!