DEV Community

Cover image for Day 62 of #100DaysOfCode — Python Refresher Part 2 + Introduction to Django
M Saad Ahmad
M Saad Ahmad

Posted on

Day 62 of #100DaysOfCode — Python Refresher Part 2 + Introduction to Django

On Day 61, I revisited the fundamentals of Python: data types, variables, functions, loops, and iteration. It was all about strengthening the core building blocks that everything else depends on.

For today, Day 62, the goal was to move beyond the basics and explore more advanced Python concepts like Object-Oriented Programming (OOP), modules and imports, virtual environments, and then begin a gradual transition into Django, a powerful web framework.


Object-Oriented Programming (OOP)

A programming paradigm based on objects that encapsulate data and behavior.

In Python, OOP allows you to structure code using classes and objects. It makes programs more modular, reusable, and easier to maintain.

Example:

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display(self):
        return f"{self.brand} {self.model}"

# Creating an object
my_car = Car("Toyota", "Corolla")
print(my_car.display())
Enter fullscreen mode Exit fullscreen mode

Here:

  • Car is a class
  • my_car is an object (instance)
  • Methods define behavior, attributes store data

Modules and Imports

A module is a file containing Python code (functions, variables, classes) that can be reused.

Modules help organize code and promote reusability.

Example:

# math_utils.py
def add(a, b):
    return a + b
Enter fullscreen mode Exit fullscreen mode
# main.py
import math_utils

result = math_utils.add(3, 5)
print(result)
Enter fullscreen mode Exit fullscreen mode

You can also import specific functions:

from math_utils import add
Enter fullscreen mode Exit fullscreen mode

Virtual Environments

They are isolated environments for managing project-specific dependencies.

They prevent conflicts between packages across different projects.

Example:

# Create a virtual environment
python -m venv myenv

# Activate it
# On Windows:
myenv\Scripts\activate

# On Mac/Linux:
source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Once activated, you can install packages locally:

pip install django
Enter fullscreen mode Exit fullscreen mode

Quick Note on Modules & Imports

Modules and imports are essential for scaling Python projects; they keep your code clean, modular, and easier to maintain as complexity grows.


Introduction to Django

Django is a high-level Python web framework used to build secure, scalable, and maintainable web applications quickly.

It’s widely used for:

  • Backend web development
  • REST APIs
  • Database-driven applications

Django follows the “batteries-included” philosophy, meaning it comes with many built-in features like authentication, admin panel, and ORM.


⚙️ Core Django Concepts

Project Structure

A Django project is the overall application container. It includes settings, configurations, and multiple apps.

myproject/
│── manage.py
│── myproject/
│   │── __init__.py
│   │── settings.py
│   │── urls.py
│   │── asgi.py
│   │── wsgi.py
Enter fullscreen mode Exit fullscreen mode
  • settings.py → Contains all project configurations (apps, database, middleware, etc.)
  • urls.py → Defines the main URL routes for the entire project
  • manage.py → Command-line utility to run and manage the project

Apps in Django

Apps are modular components within a project. Each app handles a specific functionality (e.g., blog, users, payments).

myproject/
│── blog/
│   │── models.py
│   │── views.py
│   │── admin.py
│   │── apps.py
Enter fullscreen mode Exit fullscreen mode
  • models.py → Defines the database structure using Python classes
  • views.py → Contains the logic that handles requests and returns responses
  • admin.py → Registers models to manage them via Django’s admin panel

URLs and Routing

Django uses URL patterns to map web requests to specific views (functions or classes that handle logic).

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]
Enter fullscreen mode Exit fullscreen mode
# views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django!")
Enter fullscreen mode Exit fullscreen mode

Django ORM

The Object-Relational Mapper (ORM) allows you to interact with databases using Python instead of SQL.

Example:

# Instead of writing SQL
# SELECT * FROM users;

# You can do:
User.objects.all()
Enter fullscreen mode Exit fullscreen mode

Conclusion

Day 62 was all about leveling up from Python basics to more structured and scalable coding practices. Concepts like OOP, modules, and virtual environments are essential for writing clean and maintainable code.

The introduction to Django marked the beginning of a new phase, moving from scripting to building real-world web applications. Looking forward to diving deeper into Django in the coming days.

Thanks for reading. Feel free to share your thoughts!

Top comments (0)