Table of Contents
Table of Contents
Django is a popular and open-source web framework built with Python that allows developers to build web applications quickly and efficiently. It emphasizes clean, maintainable code, making it easier to scale and manage complex applications. Django simplifies many of the common tasks involved in web development, such as handling user authentication, managing databases, and building reusable components.
This guide will walk you through the process of installing Django on an Ubuntu system and show you how to create your first Django project. Whether you’re a beginner or an experienced developer, this tutorial will help you get started with Django in no time.
Django runs on Python, and Ubuntu typically comes with Python pre-installed. If you have a minimal Ubuntu installation, Python may not be installed by default. First, let’s make sure Python and its package manager, pip, are installed:
Open a terminal and update your package list:
apt update
Install Python 3 and pip3:
apt install python3 python3-pip
Verify the Python installation:
python3 –version
You should see something like:
Now that Python is installed, you can easily install Django using python3, the Python package manager:
apt install python3-django
After the installation, verify the version of Django that was installed:
django-admin –version
4.2.11
With Django installed, you can create your first Django project. This project will act as the foundation for your web application.
Navigate to the directory where you want to create the project. For example:
cd /home/your_username/projects
Use the django-admin command to start a new project. Replace with django_app your desired project name:
django-admin startproject django_app
Enter the project directory and apply the initial migrations:
cd django_app
python3 manage.py migrate
This sets up the database structure that Django uses to store data.
To manage your application through Django’s powerful admin interface, you’ll need to create a superuser account.
In your project directory, run the following command to create a superuser:
python3 manage.py createsuperuser
You will be prompted to enter a username, email, and password for the superuser. Make sure to remember this information, as you’ll need it to log in to the admin panel.
By default, Django only allows access to your web application from the local machine (localhost). To make your application accessible from other devices, you need to modify the ALLOWED_HOSTS setting in the project’s settings.py file.
vi django_app/settings.py
Locate the line that says ALLOWED_HOSTS = [] and add your server’s IP address or domain name. For example:
Replace your-server-ip with your actual server IP or domain,Save and exit the file.
Now that your Django project is configured, you can run the development server to view the application.
python3 manage.py runserver 0.0.0.0:8000
This will make your project accessible on all network interfaces at port 8000. You can change the port number if needed.
In your web browser, navigate to:
You should see the default Django welcome page.
Django provides an admin interface for managing your project. Once the server is running, you can access this interface using the /admin URL.
The Django admin dashboard looks like below. Here you can add more users and groups for your application
Congratulations! You have successfully installed Django on your Ubuntu system and set up your first Django project. Django is a versatile and robust framework, and you are now ready to start developing your web applications.
Explore more of Django’s features, such as creating models, views, templates, and forms, to build a fully functional web application. The official Django documentation is a great resource for learning more about the framework. Happy coding!