Practice: Django Fundamentals
Q1. Count url patterns
Create a list urlpatterns holding three strings: home, about, contact. Print how many there are.
Goal: Work with URL lists.
Q2. Simple view function
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
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
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
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
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
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
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
What does this print?
routes = {"/": "home", "/about": "about page"}
print(routes.get("/about"))
print(routes.get("/missing", "404"))Goal: Read a route table.
Q10. Output: named group
What does this print?
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
What does this print?
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
What does this print?
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
What does this print?
steps = []
for name in ["makemigrations", "migrate"]:
steps.append(name.upper())
print(" -> ".join(steps))Goal: Recall the migration workflow.
Q14. Debug: wrong command
This list of setup steps puts migrate first. Fix the order so the program prints the correct sequence.
steps = ["migrate", "makemigrations"]
print(", ".join(steps))Goal: Order the migration commands.
Q15. Debug: missing return
The view prints text instead of returning it, so the caller prints None. Fix it.
def about(request):
print("About us")
print(about(None))Goal: Return a response from a view.
Q16. Debug: string number
The captured URL value is text, so the sum is wrong. Fix it so the program prints 11.
pk = "10"
print(pk + 1)Goal: Convert captured values.
Q17. Project versus app
Explain the difference between a Django project and a Django app.
Goal: Separate projects and apps.
Q18. What is MTV
Describe the role of Model, Template and View in Django in one sentence each.
Goal: Explain the MTV pattern.
Q19. Why migrations
Why does Django need migrations after you edit a model?
Goal: Explain migrations.
Q20. Flask or Django
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.