James Williams
# Create virtual environment
python -m venv venv
# Activate (Windows)
venv\Scripts\activate
# Activate (Mac/Linux)
source venv/bin/activate
# Install Django
pip install django
# Verify installation
python -m django --version
# Create project
django-admin startproject myproject
# Project structure:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
# Run development server
python manage.py runserver
# Visit: http://127.0.0.1:8000/
# Create app
python manage.py startapp blog
# Register app in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
# ...
'blog', # Add your app
]
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return HttpResponse("<h1>Welcome to CMM721!</h1>")
def about(request):
context = {'course': 'CMM721'}
return render(request, 'about.html', context)
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
Create a personal portfolio website using Django
<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Welcome {{ user.username }}!</h1>
<p>{{ content }}</p>
{% for item in items %}
<div>{{ item.name }}</div>
{% endfor %}
</body>
</html>
{{ variable }}
{{ user.name }}
{{ items.0.title }}
{% if user.is_authenticated %}
Welcome {{ user.username }}!
{% else %}
Please log in.
{% endif %}
{% for post in posts %}
<h2>{{ post.title }}</h2>
{% empty %}
<p>No posts yet.</p>
{% endfor %}
{{ text|lower }}
{{ date|date:"Y-m-d" }}
{{ content|truncatewords:30 }}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}CMM721{% endblock %}</title>
</head>
<body>
<nav>Navigation here</nav>
{% block content %}{% endblock %}
<footer>Footer here</footer>
</body>
</html>
{% extends 'base.html' %}
{% block title %}Home - CMM721{% endblock %}
{% block content %}
<h1>Welcome Home</h1>
<p>Page content here</p>
{% endblock %}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<img src="{% static 'images/logo.png' %}">
<script src="{% static 'js/script.js' %}"></script>
static/
css/
style.css
js/
script.js
images/
logo.png
Enhance portfolio with Django templates and static files
# settings.py
SECRET_KEY = 'your-secret-key'
DEBUG = True # Set False in production
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
INSTALLED_APPS = [...]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
TIME_ZONE = 'Europe/London'
LANGUAGE_CODE = 'en-gb'
# Run development server
python manage.py runserver
# Create database migrations
python manage.py makemigrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Start new app
python manage.py startapp myapp
# Run tests
python manage.py test
# Collect static files
python manage.py collectstatic
# Open Django shell
python manage.py shell
Preparation: Complete Task 1 and 2. Review Python classes.
Contact: JWilliams@Staff.newman.ac.uk
Office Hours: By appointment
Resources: Django Documentation, Django Girls Tutorial
Thank you for your attention!