Welcome, aspiring web developers! Have you ever wanted to create something interactive and fun, even if you’re just starting your journey into web development? Today, we’re going to combine the power of Django – a fantastic web framework – with some client-side magic to build a super simple, interactive drawing application. This project falls into our “Fun & Experiments” category because it’s a great way to learn basic concepts while seeing immediate, visible results.
By the end of this guide, you’ll have a basic webpage where you can draw directly in your browser using your mouse. It’s a perfect project for beginners to understand how Django serves web pages and how client-side JavaScript can bring those pages to life!
What is Django?
Before we dive in, let’s quickly understand what Django is.
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Think of it as a toolkit that helps you build powerful websites quickly, taking care of many common web development tasks so you can focus on your unique application.
Setting Up Your Environment
First things first, let’s get your computer ready. We’ll assume you have Python and pip (Python’s package installer) already installed. If not, please install Python from its official website.
It’s good practice to create a virtual environment for each project. A virtual environment is like an isolated space for your project’s dependencies, preventing conflicts between different projects.
-
Create a virtual environment:
Navigate to the folder where you want to create your project in your terminal or command prompt.
bash
python -m venv venvpython -m venv: This command uses Python’s built-invenvmodule to create a virtual environment.venv: This is the name we’re giving to our virtual environment folder.
-
Activate the virtual environment:
- On macOS/Linux:
bash
source venv/bin/activate - On Windows:
bash
venv\Scripts\activate
You’ll know it’s active when you see(venv)at the beginning of your terminal prompt.
- On macOS/Linux:
-
Install Django:
With your virtual environment active, install Django usingpip.
bash
pip install Django
Starting a New Django Project
Now that Django is installed, let’s create our project and an app within it. In Django, a project is a collection of settings and apps that together make up a complete web application. An app is a web application that does something specific (e.g., a blog app, a drawing app).
-
Create the Django project:
Make sure you are in the same directory where you created your virtual environment.
bash
django-admin startproject mysketchbook .django-admin: This is Django’s command-line utility.startproject mysketchbook: This tells Django to create a new project namedmysketchbook..: This is important! It tells Django to create the project files in the current directory, rather than creating an extra nestedmysketchbookfolder.
-
Create a Django app:
bash
python manage.py startapp drawingapppython manage.py:manage.pyis a script automatically created with your project that helps you manage your Django project.startapp drawingapp: This creates a new app nameddrawingappwithin yourmysketchbookproject. This app will contain all the code specific to our drawing functionality.
Integrating Your App into the Project
For Django to know about your new drawingapp, you need to register it in your project’s settings.
-
Edit
mysketchbook/settings.py:
Open themysketchbook/settings.pyfile in your code editor. Find theINSTALLED_APPSlist and add'drawingapp'to it.“`python
mysketchbook/settings.py
INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘drawingapp’, # Add your new app here
]
“`
Basic URL Configuration
Next, we need to tell Django how to direct web requests (like someone typing /draw/ into their browser) to our drawingapp. This is done using URLs.
-
Edit the project’s
mysketchbook/urls.py:
This file acts as the main dispatcher for your project. We’ll include our app’s URLs here.“`python
mysketchbook/urls.py
from django.contrib import admin
from django.urls import path, include # Import includeurlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘draw/’, include(‘drawingapp.urls’)), # Direct requests starting with ‘draw/’ to drawingapp
]
``include(‘drawingapp.urls’)
*: This means that any request starting with/draw/will be handed over to theurls.pyfile inside yourdrawingapp` for further processing. -
Create
drawingapp/urls.py:
Now, create a new file namedurls.pyinside yourdrawingappfolder (drawingapp/urls.py). This file will define the specific URLs for your drawing application.“`python
drawingapp/urls.py
from django.urls import path
from . import views # Import views from the current appurlpatterns = [
path(”, views.draw_view, name=’draw_view’), # Map the root of this app to draw_view
]
``path(”, views.draw_view, name=’draw_view’)
*: This tells Django that when a request comes to the root of ourdrawingapp(which is/draw/because of our project'surls.py), it should call a function nameddraw_viewfrom ourviews.pyfile.name=’draw_view’` gives this URL a handy name for later use.
Creating Your View
A view in Django is a function that takes a web request and returns a web response, typically an HTML page.
-
Edit
drawingapp/views.py:
Opendrawingapp/views.pyand add the following code:“`python
drawingapp/views.py
from django.shortcuts import render
def draw_view(request):
“””
Renders the drawing application’s main page.
“””
return render(request, ‘drawingapp/draw.html’)
``render(request, ‘drawingapp/draw.html’)
*: This function is a shortcut provided by Django. It takes the incomingrequest, loads the specified **template** (drawingapp/draw.html`), and returns it as an HTTP response. A template is essentially an HTML file that Django can fill with dynamic content.
Crafting Your Template (HTML, CSS, and JavaScript)
This is where the magic happens on the user’s browser! We’ll create an HTML file that contains our drawing canvas, some styling (CSS), and the JavaScript code to make the drawing interactive.
-
Create the templates directory:
Inside yourdrawingappfolder, create a new folder namedtemplates. Insidetemplates, create another folder nameddrawingapp. This structure (drawingapp/templates/drawingapp/) is a common Django convention that helps keep your templates organized and prevents name clashes between different apps. -
Create
drawingapp/templates/drawingapp/draw.html:
Now, create a file nameddraw.htmlinsidedrawingapp/templates/drawingapp/and paste the following code:“`html
<!DOCTYPE html>
Simple Drawing App