mwalalearn-Django Introduction

Objectives: Django Introduction

Django Introduction

Introduction to Django

What is Django?

Django is a Python framework that makes it easier to create websites and web applications. It takes care of difficult parts like authentication, database connections, and CRUD operations (Create, Read, Update, Delete), so you can focus on your main application logic.

Real-life Example:

Think of Django as the foundation of a house. Instead of starting by making bricks and cement from scratch, Django gives you ready-made bricks (login system, database tools, admin panel) so you can directly build your house (the web application).

Advice:

When learning Django, first make sure you are comfortable with Python basics (functions, classes, loops). This will make understanding Django much easier.

How Does Django Work?

Django follows the MVT design pattern (Model-View-Template):

  • Model: Handles data from the database.
  • View: Handles user requests and decides what data to show.
  • Template: Defines how the data will be displayed (HTML pages).

Model

Models provide data using Object Relational Mapping (ORM), so you don’t need to write complex SQL queries.

Real-life Example:

If you want to show student records on a website, instead of writing raw SQL like SELECT * FROM students WHERE grade='A', in Django you simply write: Student.objects.filter(grade="A").

Advice:

When starting, avoid jumping straight into advanced ORM queries. Begin with simple create and fetch operations. Build confidence step by step.

View

A view is a function or method that processes requests, fetches the right data, and passes it to a template.

Real-life Example:

When you log into Facebook and request your profile page, a Django view fetches your personal data and sends it to the template that displays your profile.

Advice:

Keep your views simple! If they get too long, move complex logic to models or helper functions.

Template

Templates are usually HTML files that describe the layout of a web page. Django allows you to add dynamic data with template tags like {{ firstname }}.

Real-life Example:

A template for a shopping site might look like this:

<h1>Welcome {{ customer_name }}</h1>
<p>You have {{ cart_items }} items in your cart.</p>

Advice:

Keep design (HTML/CSS) inside templates and avoid putting too much logic in them. Heavy logic belongs in views or models.

URLs

Django uses urls.py to map URLs to views.

Real-life Example:

If a user goes to /about, Django checks urls.py to see which view should handle it. For example, path("about/", views.aboutPage).

Advice:

Organize your URLs clearly. Use namespaces if you have many apps inside one Django project.

How Everything Works Together

  1. User requests a URL.
  2. Django checks urls.py to find the matching view.
  3. The view (from views.py) fetches data from the model.
  4. The view sends this data to a template.
  5. The template creates the final HTML and sends it back to the browser.

Real-life Example:

When you search for a book in an online library website:

  • You type the book name (URL request).
  • Django routes it to the search view.
  • The view checks the database for matching books.
  • Results are sent to a template.
  • You see a styled list of matching books on your screen.

Django History

Django was invented in 2003 at Lawrence Journal-World newspaper to build web applications quickly. It was released to the public in 2005. The latest stable version (as of March 2022) is Django 4.0.3.

Advice:

Don’t worry too much about the history — focus on practicing. Start with small apps like a To-Do list or Blog. Learning by doing is the best way to master Django.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::