Introduction to Python and Django Basics
From the https://youtu.be/HTtd1d--WsM?si=kj3jDopaEoScmQdF curriculum
TL;DR
Python is a popular, easy-to-read programming language that's great for building web applications and more. Django is a powerful web framework built with Python, providing tools and structure to help you create complex web apps quickly. Together, they form a robust combination for developing dynamic, database-driven websites.
1. The Mental Model
Think of Python as your general-purpose toolbox, capable of many tasks. Django is like a specialized workbench within that toolbox, specifically designed for building websites, providing pre-made components and a clear layout for your project.
2. The Core Material
Python is known for its simplicity and readability, making it a good choice for beginners. You write Python code in files, and a Python interpreter runs this code. It handles everything from simple calculations to complex data manipulation.
Django is a "batteries-included" framework, meaning it comes with many features you'd expect in a web application built-in, like user authentication, an admin interface, and database management. It follows the Model-View-Controller (MVC) architectural pattern, though Django often refers to it as Model-View-Template (MVT).
Here's how Django typically handles a web request:
graph TD
A["User's Browser (Request)"] --> B["Django Server"];
B --> C["URLs.py (URL Router)"];
C -- "Matches URL Pattern" --> D["Views.py (Business Logic)"];
D -- "Interacts with data" --> E["Models.py (Database)"];
E -- "Data from DB" --> D;
D -- "Prepares data for display" --> F["Templates (HTML/CSS)"];
F --> G["User's Browser (Response)"];
Key Django Components:

Photo by Nic Wood on Pexels
- Models: These are Python classes that define the structure of your data and interact with your database. Each model usually maps to a table in your database.
- Views: These are Python functions or classes that receive web requests, interact with models to get or save data, and then render a template or return an HTTP response. They contain the business logic.
- Templates: These are text files (often HTML) with special Django template tags that allow you to insert dynamic data from your views. They define how your web page looks.
- URLs: This file (typically
urls.py) maps specific URLs (web addresses) to specific views. When a request comes in, Django checks this file to see which view should handle that URL.
Setting Up a Django Project:

Photo by Pixabay on Pexels
- Install Python: Make sure you have Python installed. You can check with
python --versionorpython3 --version. - Create a Virtual Environment: This isolates your project's dependencies.
bash python -m venv myprojectenv - Activate Virtual Environment:
- On Windows:
myprojectenv\Scripts\activate - On macOS/Linux:
source myprojectenv/bin/activate
- On Windows:
- Install Django:
bash pip install Django - Create a New Django Project: This sets up the basic file structure.
bash django-admin startproject myproject .
(The.means create it in the current directory) - Create an App: Django projects are made of "apps," which are self-contained modules for specific functionalities (e.g., a "blog" app, a "users" app).
bash python manage.py startapp myapp - Run Migrations: Django uses migrations to create database tables based on your models.
bash python manage.py migrate - Run the Development Server:
bash python manage.py runserver
This starts a local web server, usually accessible athttp://127.0.0.1:8000/.
3. Worked Example
Let's create a simple "Hello, World!" page using Django.
Assuming you've followed the setup steps and have a myproject project and a myapp app:
-
Add
myapptoINSTALLED_APPS: Openmyproject/settings.pyand add'myapp'to theINSTALLED_APPSlist:
python # myproject/settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', # Add your app here ] -
Create a View: Open
myapp/views.pyand add:
```python
# myapp/views.py
from django.http import HttpResponsedef hello_world(request):
return HttpResponse("Hello, World from Django!
")
``` -
Define URLs for
myapp: Insidemyapp/, create a new file namedurls.pyand add:
```python
# myapp/urls.py
from django.urls import path
from . import viewsurlpatterns = [
path('hello/', views.hello_world, name='hello_world'),
]
``` -
Include
myapp's URLs in the Project URLs: Openmyproject/urls.pyand add anincludestatement:
```python
# myproject/urls.py
from django.contrib import admin
from django.urls import path, include # Import includeurlpatterns = [
path('admin/', admin.site.urls),
path('app/', include('myapp.urls')), # Include your app's URLs
]
`` Now, any URL starting with/app/will be handled bymyapp'surls.py`. -
Run the Server:
bash python manage.py runserver
Navigate your browser tohttp://127.0.0.1:8000/app/hello/. You should see "Hello, World from Django!".
4. Key Takeaways
- Python is a versatile, high-level programming language known for its clear syntax.
- Django is a powerful, full-stack Python web framework that simplifies web development.
- Django follows a Model-View-Template (MVT) pattern, structuring your code logically.
- A Django project can contain multiple "apps," which are self-contained modules.
- Virtual environments are crucial for managing project dependencies and avoiding conflicts.
django-adminis used for project setup, andpython manage.pyfor app-specific commands.
Common mistakes to avoid:
- Forgetting to activate your virtual environment before installing packages or running Django commands.
- Not adding your new Django app to INSTALLED_APPS in settings.py.
- Forgetting to include your app's urls.py in the main project's urls.py.
- Not running python manage.py migrate after creating or changing models.
- Trying to run Django without Django installed (or without it installed in the active virtual environment).
5. Now Try It
Modify the "Hello, World!" example. Instead of just displaying "Hello, World!", create a new view in myapp/views.py called greet_name that takes a name from the URL (e.g., http://127.0.0.1:8000/app/greet/Alice/) and displays "Hello, Alice!". You'll need to update myapp/urls.py to include a URL pattern that captures a string from the URL.
Success looks like: Navigating to http://127.0.0.1:8000/app/greet/YourName/ in your browser and seeing "Hello, YourName!" (where "YourName" is whatever you typed in the URL).
Frequently asked about Introduction to Python and Django Basics
Study this next
Get the full https://youtu.be/HTtd1d--WsM?si=kj3jDopaEoScmQdF curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free