Practice: Django Fundamentals

« Read the chapter

Q1. Count url patterns

BeginnerCoding

Create a list urlpatterns holding three strings: home, about, contact. Print how many there are.

Goal: Work with URL lists.

Q2. Simple view function

BeginnerCoding

Write a function home(request) that returns Welcome to Django style views, then call it with None and print the result.

Goal: Write a view function.

Q3. Context dictionary

BeginnerCoding

Build a context dictionary with keys title (Shop) and items (2). Print the title using the dictionary.

Goal: Use the template context.

Q4. Model as a class

IntermediateCoding

Write a class Book with a title attribute set in __init__ and a __str__ method returning the title. Print one Book.

Goal: Write model like classes.

Q5. Filter by pages

IntermediateCoding

Given books as a list of (title, pages) tuples, print the titles with at least 200 pages, in a list.

Goal: Filter data like an ORM.

Q6. Path matcher

IntermediateCoding

Write a function match(path) that returns Home for / and Not found for anything else. Print the result for / and /x.

Goal: Mimic URL resolving.

Q7. Count with sqlite

AdvancedCoding

Using sqlite3 in memory, create a table t with one column n, insert 3 rows and print the row count.

Goal: Use a database as the ORM does.

Q8. Order the rows

AdvancedCoding

Using sqlite3 in memory insert the names Zed, Amy, Bob in one column and print them in alphabetical order as a list.

Goal: Sort query results.

Q9. Output: dispatcher

BeginnerOutput

What does this print?

Code
routes = {"/": "home", "/about": "about page"}
print(routes.get("/about"))
print(routes.get("/missing", "404"))

Goal: Read a route table.

Q10. Output: named group

IntermediateOutput

What does this print?

Code
import re
m = re.match(r"^books/(?P<pk>[0-9]+)/$", "books/42/")
print(m.groupdict())
print(int(m.group("pk")) + 1)

Goal: Understand captured URL values.

Q11. Output: template

BeginnerOutput

What does this print?

Code
from string import Template
print(Template("Hi $name, you have $n items").substitute(name="Li", n=4))

Goal: Fill template placeholders.

Q12. Output: class state

IntermediateOutput

What does this print?

Code
class Post:
    count = 0
    def __init__(self, title):
        self.title = title
        Post.count += 1

Post("a")
Post("b")
print(Post.count)

Goal: Track class level data.

Q13. Output: migration order

AdvancedOutput

What does this print?

Code
steps = []
for name in ["makemigrations", "migrate"]:
    steps.append(name.upper())
print(" -> ".join(steps))

Goal: Recall the migration workflow.

Q14. Debug: wrong command

BeginnerDebugging

This list of setup steps puts migrate first. Fix the order so the program prints the correct sequence.

Code
steps = ["migrate", "makemigrations"]
print(", ".join(steps))

Goal: Order the migration commands.

Q15. Debug: missing return

IntermediateDebugging

The view prints text instead of returning it, so the caller prints None. Fix it.

Code
def about(request):
    print("About us")

print(about(None))

Goal: Return a response from a view.

Q16. Debug: string number

IntermediateDebugging

The captured URL value is text, so the sum is wrong. Fix it so the program prints 11.

Code
pk = "10"
print(pk + 1)

Goal: Convert captured values.

Q17. Project versus app

BeginnerConceptual

Explain the difference between a Django project and a Django app.

Goal: Separate projects and apps.

Q18. What is MTV

BeginnerConceptual

Describe the role of Model, Template and View in Django in one sentence each.

Goal: Explain the MTV pattern.

Q19. Why migrations

IntermediateConceptual

Why does Django need migrations after you edit a model?

Goal: Explain migrations.

Q20. Flask or Django

IntermediateApplication

A client wants a small blog with logins, an editor area and a database in one week. Which framework would you pick and why?

Goal: Choose a framework.