Introduction
Django is a versatile web development framework. Many top companies, including Instagram, FireFox and Spotify use the framework to develop their websites. Django is useful when developing complex, large-scale web apps. However, many developers use it to create simple web apps. This tutorial guides new Django developers on how to set up a Django development environment to create their web apps. The tutorial will guide on how to set up a Django app in both Windows and Linux environments.
Prerequisites: Ensure you have a Python version 3.4+ installed on your system.
Setting up a Django application involves two major steps:
Setting up a Python virtual environment
Starting a Django project
Jump to Windows set-up
Jump to Linux set-up
Windows set-up
This set-up assumes you are running Windows 10
Follow these steps for Windows set up:
-
Create a project folder in your windows file system.
For example, create the folder named django_app in your desktop directory
-
Start a Windows terminal
- Press
Win + R
to open a dialog box - Type
cmd
then pressenter
- Press
-
Navigate to the project folder
cd Desktop\django_app
Create a virtual environment
Runpython -m venv my_env
Note: my_env is a custom name you give to your virtual environmentActivate the virtual environment
Run.\my_env\Scripts\activate
-
Install Django using pip
pip install django
Start a Django project
Rundjango-admin startproject django_app .
Note: django_app is a custom name you give to your project.
Do not forget the dot at the end of the command or you may run into some configurations problems when deploying your app.-
Change into the Django project directory
cd django_app
Create your Django application
Runpython manage.py startapp my_first_app
Note: my_first_app is a custom name of the specific Django application you are creating-
Add your application in the project's settings.py file
- Locate a file named settings.py within your project folder. Hint: You can locate it manually within your project folder. It should be in a subdirectory with a similar name to your project.
- Open the settings.py file using a code editor
- Locate a section called INSTALLED_APPS
- Add the name of your Django application at the end
- Save and close the settings.py file
-
Create your app's database schema
- Return to your terminal
- Run
python manage.py migrate
Note: It is necessary to run this operation to create a database that the app can work with
Start a development server
In your terminal, with the virtual environment still active, runpython manage.py runserver
Note: The operation will start a local development server on port 8000View your project in a browser
Open a browser and enter the URL http://127.0.0.1:8000/
Linux set-up
This set up assumes you are running an ubuntu version 20.04+
Open an ubuntu shell on your system
-
Create a project directory and switch into the directory
mkdir django-project && cd django-project
-
Create a virtual environment
python3 -m venv my_env
-
Activate the virtual environment
source my_env/bin/activate
-
Install Django using pip
pip install django
To install a specific Django version
pip install django==3.2.21
-
Start a Django project
django-admin startproject django_project
-
Switch into the Django project directory
cd django_project
-
Start a Django application
python3 manage.py startapp django_app
-
Add the new application to your settings.py file
- Locate a file, settings.py within your shell
- Open the file using a code editor
- Locate a section named INSTALLED_APPS
- Add
'django_app'
at the end of installed apps list - Save and close the settings.py file
-
Create a database
python3 manage.py migrate
-
Start a development server on port 8000
python3 manage.py runserver
View your project in a browser
Run http://127.0.0.1:8000/
Conclusion
By following either steps depending on your operating system, you will have created a basic Django web application. Note that this is only a basic application, without any features. You need to build on the basic application by customizing it further to suit your project's needs or client's specifications
Top comments (0)