Django Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Python You Need: Classes
Exercise
Inherit from Animal.
class Dog():
Python You Need: Decorators
Exercise
Apply a decorator.
shout\ndef f(): passDjango Introduction
Exercise
Name the language.
Django is written in Install Django
Exercise
Install the package.
pip install Create a Project
Exercise
Start the dev server.
python manage.py Project Structure
Exercise
Which file holds settings?
.pyCreate an App
Exercise
Register an app.
Add "blog" to Your First View
Exercise
What does a view receive?
def index():
URL Routing
Exercise
Include another URLconf.
path("blog/", ("blog.urls"))
URL Parameters
Exercise
Capture an integer.
path("post/<:id>/", view)
Named URLs and reverse
Exercise
Build a URL from a name.
("detail", args=[5])Templates
Exercise
Render a template.
return (request, "a.html", ctx)
Variables
Exercise
Print a variable.
name }}Tags: if and for
Exercise
Close a for loop.
{% for x in xs %}...{% %}
Filters
Exercise
Use a fallback value.
{{ x|:"none" }}
Template Inheritance
Exercise
Use a parent.
{% "base.html" %}
Static Files
Exercise
Load the helper.
{% load %}
Include and Comments
Exercise
Insert a partial.
{% "partials/nav.html" %}
Rendering Templates
Exercise
Render.
return (request, "a.html")
Template Variables
Exercise
Print a variable.
{{ }}
Template Tags
Exercise
Loop tag.
{% p in posts %}
Template Filters
Exercise
Default value.
{{ n|:"x" }}
Template Inheritance
Exercise
Extend.
{% "base.html" %}
The include Tag
Exercise
Include a file.
{% "nav.html" %}
The url Tag
Exercise
Build a link.
{% "home" %}
Static Files
Exercise
Load tag.
{% load %}
CSRF Protection
Exercise
Token tag.
{% %}
Auto Escaping
Exercise
Mark safe.
{{ x| }}
Template Settings
Exercise
App folders.
"": True
Models
Exercise
Base class of models.
class Post(models.):
Field Types
Exercise
Make a value optional in forms.
note = models.TextField(=True)
Migrations
Exercise
Apply migrations.
python manage.py The ORM in the Shell
Exercise
Save an object.
p.()
QuerySets
Exercise
Newest first.
order_by("created")
Field Lookups
Exercise
Case insensitive contains.
title__="x"
Update and Delete
Exercise
Remove objects.
Post.objects.filter(id=3).()
Relationships: ForeignKey
Exercise
Delete children with parent.
on_delete=models.Many to Many and One to One
Exercise
Link many to many.
tags = models.Field(Tag)
__str__ and Meta
Exercise
Set a text label.
def (self): return self.title
The Admin Site
Exercise
Create the first admin user.
python manage.py Customising the Admin
Exercise
Columns in the list.
list = ("title",)
Forms
Exercise
Email input field.
email = forms.Field()
Handling Forms in Views
Exercise
Check validity.
if form.():
ModelForms
Exercise
Build a form from a model.
class PostForm(forms.Form):
Form Validation
Exercise
Reject bad input.
raise forms.Error("bad")
Shortcuts: get_object_or_404 and redirect
Exercise
Return 404 if missing.
get_object_or_(Post, pk=1)
JsonResponse and HTTP Methods
Exercise
Return JSON.
return Response({"ok": True})
Class Based Views
Exercise
Use a class view in urls.
HelloView.()
ListView and DetailView
Exercise
View for many objects.
class PostList():
The Messages Framework
Exercise
Show a success message.
messages.(request, "Saved")
Sessions and Cookies
Exercise
Read a session value.
request..get("cart")
CreateView, UpdateView and DeleteView
Premium exercise. See plans
Authentication: Login and Logout
Premium exercise. See plans
User Registration
Premium exercise. See plans
Permissions and Groups
Premium exercise. See plans
Custom User Model
Premium exercise. See plans
Middleware
Premium exercise. See plans
Signals
Premium exercise. See plans
select_related and prefetch_related
Premium exercise. See plans
Aggregation and Annotation
Premium exercise. See plans
Q and F Objects
Premium exercise. See plans
Custom Managers and QuerySets
Premium exercise. See plans
Database Transactions
Premium exercise. See plans
Caching
Premium exercise. See plans
File Uploads
Premium exercise. See plans
Django REST Framework Basics
Premium exercise. See plans
API Authentication and Pagination
Premium exercise. See plans
Testing
Premium exercise. See plans
Async Views
Premium exercise. See plans
Custom Template Tags and Filters
Premium exercise. See plans
Custom Management Commands
Premium exercise. See plans
Settings and Environment Variables
Premium exercise. See plans
Security Checklist
Premium exercise. See plans
Using PostgreSQL
Premium exercise. See plans
Deployment with Gunicorn
Premium exercise. See plans
Static Files in Production
Premium exercise. See plans
Background Tasks with Celery
Premium exercise. See plans
Logging and Monitoring
Premium exercise. See plans
Docker for Django
Premium exercise. See plans
Django Best Practices
Premium exercise. See plans
Next Steps
Premium exercise. See plans
Virtual Environments
Exercise
Create one.
python -m .venv
Starting a Project
Exercise
Create project.
django-admin mysite
Creating an App
Exercise
Create app.
python manage.py blog
Project Structure
Exercise
Settings file.
mysite/.py
Settings
Exercise
Debug flag.
= TrueThe Dev Server
Exercise
Custom port.
runserver The MVT Pattern
Exercise
View role.
The holds logic
Your First View
Exercise
Return a response.
return ("Hi")
URL Configuration
Exercise
Map a path.
("about/", views.about)include for App URLs
Exercise
Delegate.
path("blog/", ("blog.urls"))
URL Parameters
Exercise
Integer converter.
<:pk>
Named URLs and reverse
Exercise
Build a URL.
("home")GET and POST
Exercise
Check method.
request. == "POST"
Query Strings
Exercise
Default value.
request.GET.get("q", )
Redirects
Exercise
Send elsewhere.
return ("home")
JSON Responses
Exercise
JSON.
return ({"a": 1})
Handling 404
Exercise
Shortcut.
(Post, pk=pk)Class Based Views
Exercise
Register a class view.
Hello.()
Generic Views
Exercise
List page.
class PostList():
Defining a Model
Exercise
Base class.
class Post(models.)
Field Types
Exercise
Yes or no field.
models.Field()
Migrations
Exercise
Apply.
python manage.py Creating Objects
Exercise
Create and save.
Post.objects.(title="A")
Reading Objects
Exercise
Filter.
Post.objects.(active=True)
Field Lookups
Exercise
Greater or equal.
views__=100
Updating and Deleting
Exercise
Delete.
p.()
Ordering and Slicing
Exercise
Newest first.
order_by("created")
exclude and Q Objects
Exercise
Exclude.
Post.objects.(x=1)
Aggregation
Exercise
Average.
("views")ForeignKey
Exercise
On delete.
on_delete=models.Reverse Relations
Exercise
Reverse name.
post.comment.all()
ManyToMany
Exercise
Add a link.
post.tags.(tag)
Model Methods
Exercise
Readable name.
def (self):
Model Meta Options
Exercise
Meta class.
class :
The Django Shell
Exercise
Open it.
manage.py Databases
Exercise
Engine.
"": "django.db.backends.sqlite3"
Admin and Superuser
Exercise
Create admin.
python manage.py superuser
Registering Models
Exercise
Register.
admin.site.(Post)
Customising ModelAdmin
Exercise
Columns.
list_ = ("title",)
Django Forms
Exercise
Email field.
forms.Field()
Using a Form in a View
Exercise
Validated data.
form.["name"]
ModelForm
Exercise
Build from a model.
class PostForm(forms.):
Rendering Forms
Exercise
Render as paragraphs.
{{ form.as }}
Custom Validation
Exercise
Raise an error.
raise forms.Error("x")
Messages Framework
Exercise
Success message.
messages.(request, "ok")
Login and Logout
Exercise
Auth urls.
include("django.contrib..urls")
Protecting Views
Exercise
Decorator.
@_required
request.user
Exercise
Auth flag.
request.user.is_
User Registration
Exercise
Built in form.
CreationFormSessions
Exercise
Session dict.
request.["n"] = 1
Middleware
Exercise
Call next layer.
self.(request)
Signals Basics
Exercise
After save.
_saveTesting
Exercise
Run tests.
manage.py Deployment Basics
Exercise
Collect.
manage.py static
Secrets and Environment
Exercise
Read env.
os.["KEY"]
Project: Blog
Exercise
Two page types.
A list and a page
Project: Todo App
Exercise
CRUD letters.
C R U Django Best Practices
Exercise
Keep views.
Keep views