Django is a free and open-source web framework written in Python that adheres to the model template view (MTV) software architectural pattern. The MTV pattern is Django’s take on the model–view–controller (MVC) pattern. According to the Django Software Foundation, the model is the single definitive source of your data, the view describes the data that gets represented to the user via a Python callback function to a specific URL, and the template is how Django generates HTML dynamically.
We will set up a Django development environment, but first, please make sure that you have a non-root user account with sudo privileges set up on a Debian or Ubuntu Linux server.
Open your terminal using:
And run the following commands:
Install Python and pip
sudo apt-get update && sudo apt-get -y upgrade
This updates the local APT repository. When prompted to configure grub-pc, you can press ENTER to accept the default, or configure as desired.
sudo apt-get install python3
Verify the successful installation of Python 3:
sudo apt-get install -y python3-pip
Verify that pip was successfully installed:
virtualenv is a virtual environment where you can install software and Python packages in a contained development space.
pip3 install virtualenv
Verify that the installation has completed successfully:
sudo pip3 install django
Verify your Django installation by running a version check:
Create a Django Test Project
Open the port we’ll be using in our server’s firewall:
sudo ufw allow 8000
Create a Directory for the Project:
While in the test-django-app directory, run the following command:
django-admin startproject testsite
Navigate to the testsite directory then list the contents of that directory to see what files were created:
You can view the manage.py script in your terminal by running the less command like so:
Navigate to the testsite directory to view the other files that were created:
You will get something like this:
Output __init__.py settings.py urls.py wsgi.py
- init.py acts as the entry point for your Python project.
- settings.py describes the configuration of your Django installation and lets Django know which settings are available.
*** urls.py** contains a urlpatterns list, that routes and maps URLs to their views.
- wsgi.py contains the configuration for the Web Server Gateway Interface.
Now we can start the server and view the website on a designated host and port by running the runserver command. Add your server ip address to the list of ALLOWED_HOSTS in the settings.py file located in
You’ll want to navigate to the Allowed Hosts Section of the document and add your server’s IP address inside the square brackets within single or double quotes.
""" Django settings for testsite project. Generated by 'django-admin startproject' using Django 1.11.3. ... """ ... # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True # Edit the line below with your server IP address ALLOWED_HOSTS = ['your-server-ip'] ...
Save the change and exit nano by holding down the CTRL + x keys and then pressing the ** y **key.
Navigate back to the directory where manage.py is located:
Replace the your-server-ip text with the IP of your server:
python3 manage.py runserver your-server-ip:8000
avigate to the below link to see what your skeleton website looks like, again replacing your-server-ip text with the IP of your server:
You should see: