Python Intermediate - Django
An Understanding of Python Frameworks
Understanding Django
Python frameworks provide developers with a structured and efficient way to build applications. Django, a robust web framework for Python, has become a go-to choice for developers aiming to build scalable and maintainable web applications. In this blog, we'll delve into the core concepts of Django, and its file structure, and guide you through creating your first Django app.
Django follows the Model-View-Template (MVT) architectural pattern, gives a reusability, less code, and rapid development environment. It comes bundled with essential components, allowing developers to focus on building features rather than reinventing the wheel.
Django’s File Structure
Before coding, let’s explore Django’s file structure. Understanding how Django organizes files is crucial for maintaining a well-structured project.
personal_blog (main project directory)
- init.py
- settings.py (core project configuration)
- urls.py (main URL patterns)
- wsgi.py (web server deployment configuration — optional)
- asgi.py (advanced — asynchronous server gateways — optional)
- manage.py (project management utility)
- README.md (project information and setup instructions — optional)
- requirements.txt (list of Python dependencies)
db.sqlite3 (database file — if using SQLite)
blog (directory for the blog application)
- init.py
- admin.py (admin interface configuration)
- apps.py (optional — blog app configuration)
- forms.py (optional — user interaction forms)
- models.py (data models for the blog app — Post, Category, Comment, etc.)
- tests.py (optional — unit tests for the blog app)
- urls.py (URL patterns for the blog app)
- views.py (Python functions handling user requests for the blog app)
- migrations (directory for database schema changes)
- init.py
- 0001_initial.py (and potentially other migration files)
templates (directory for HTML templates)
- base.html (base template for layout)
- blog (subdirectory for blog-specific templates)
- category.html (template for displaying a category of posts)
- detail.html (template for displaying a single blog post)
- index.html (template for displaying a list of all blog posts)
Building a First Django Web Application
Let’s start creating a simple Django Blog web app with the help of a link 🔗.
Start with an isolated environment
- Creating a virtual environment using
python -m venv venv
. - Activating the virtual environment
.\venv\script\activate
. - Installing Django with
python -m pip install Django
.
Creating a Django Project
- Provide the command to create a new project named
personal_blog
usingdjango-admin startproject personal_blog .
.
Starting the Development Server
- Provide the command to start the server using
python manage.py runserver
. - Mention that you can access the server at
http://localhost:8000
in your web browser.
Creating the Blog App
- Introduce the concept of Django apps and their role in project structure.
- Provide the command to create a new app named
blog
within the project usingpython manage.py startapp blog
.
Defining Blog Models
- Explain the concept of models in Django and their role in representing data.
- Briefly describe the three models used in this blog application:
Post
,Category
, andComment
.
Creating Migrations and Updating Database
- Explain the concept of migrations in Django for managing database schema changes.
- Provide the commands for creating migrations (
python manage.py makemigrations blog
) and applying them to the database (python manage.py migrate blog
).
Creating a Superuser
- Explain the need for a superuser for administrative tasks.
- Provide the command to create a superuser account with username, email, and password using
python manage.py createsuperuser
.
Registering Models with Admin
- Provide the command to register the
Post
andCategory
models with the admin usingpython manage.py createsuperuser
.
Creating Blog Views
- Briefly describe the three views created for the blog:
blog_index
,blog_detail
, andblog_category
.
Creating Blog Templates
- Explain the role of templates in Django for displaying content.
- Describe the creation of directories for blog templates:
index.html
,category.html
, anddetail.html
. - Mention the creation of a base template named
base.html
for consistent layout across child templates.
Including Routes for URLs
- Briefly explain the importance of URL patterns in mapping URLs to corresponding views.
everything works well as expected.
GitHub code
Deployment with EC2
Now that our Django app is ready, let’s deploy it using EC2, on AWS Cloud Follow these steps
Prerequisites
- An AWS account with access to EC2 services (https://aws.amazon.com/)
- Your Django project code is stored in a Git repository (like GitHub)
Launch an EC2 Instance
- Open the AWS Management Console and navigate to the EC2 service (https://aws.amazon.com/console/).
- Click on “Launch Instance”.
- Choose an Amazon Machine Image (AMI) suitable for running a Django application. A popular choice is “Amazon Linux 2 AMI”.
- Select an appropriate instance type considering your application’s memory and CPU requirements.
- Configure storage for your instance (typically an EBS volume).
- Review and configure other settings like security groups (allowing SSH access for now) and networking.
- Launch the instance.
Connect to the EC2 Instance
- Once the instance is running, note its public IP address from the EC2 console.
- Use an SSH client like PuTTY (Windows) or the terminal (Linux/macOS) to connect to the instance using its public IP address and your AWS credentials (key pair).
Install Dependencies
- Update the package list and install Python3 and pip
sudo apt update #Updates_package_lists
sudo apt install python3 #installs_Python3
sudo apt install python3-pip #installs_package_manager_for_Python
sudo apt install python3-django #installs_Django_framework_for_Python3
sudo apt install git #installs_git_to_access_github
Deploy Your Code
- There are several ways to deploy your code choose which you are comfortable with
- Git: Clone your Git repository onto the EC2 instance using
git clone
.
- Manual Upload: Upload your project files to the instance using SCP or a file transfer tool.
- Unzip the project files (if needed) and navigate to the project directory.
Allow Host
- In path personal_blog/settings.py, add *, target domain name or IP Address of server
Migrate Data from SQLite File
Python3 manage.py makemigrations
Python3 manage.py migrate
Run the Development Server
- Since we are not using a database externally, you can directly run the development server. However, keep in mind that the development server is not ideal for production environments.
python3 manage.py runserver 0.0.0.0:8000
This starts the development server …
Note: Check over inbound and outbound rules in the security group if any error occurs.
Accessing the Application
- Since you’re running locally on the EC2 instance, you can access your application in a web browser on the EC2 instance itself by navigating to
- Blog App Home Page:
http://Ec2_IP_Address:8000/
- Admin Panel:
http://Ec2_IP_Address:8000/admin
Congratulations! You’ve successfully built and deployed your first Django app also deployed in EC2.
Django is a big framework so it contains several possible ways of understanding and deployment also it takes a hard time to learn it’s just an overview!!!, so it can seem overwhelming at first.
In the next blog post, we’ll explore another framework and create a simple app, continuing our journey into the exciting world of Python development. Stay tuned!
Happy coding!
Reach me 👨💻