Python
Web with Django
46 flashcards · answers and spaced-repetition review in the KnowCard app
In Django's MVT pattern, which component plays the role that a controller plays in classic MVC?
A Django view function ends with template.render(context) but nothing reaches the browser. The render result is computed and discarded. What's the fix?
What does this print?
```python
qs = Article.objects.filter(
title__startswith='D')
print(type(qs).__name__)
```
True or false: this line runs a SQL query against the database.
```python
qs = Article.objects.filter(
text__startswith='Finally')
```
You added a price field to a Django model and reload the site, but the DB has no price column — errors everywhere. You already ran migrate. What did you skip?
In Django, what's the difference between a "project" and an "app", and which one do you create with startapp?
You ran startapp news and wrote your models, but Django acts like the app doesn't exist (no admin entry, no migrations). What's missing?
What is each attribute of a Django model class (e.g. title = models.CharField(...)), and what does Django generate from it?
Why does urls.py exist in a Django project — what does it actually do, and what does it NOT do?
What does this URL pattern pass to the view, and as what type?
```python
path('<int:article_id>/',
views.articles_detail)
```
Request: /articles/2/
Where should request handling and DB queries live vs. presentation logic, in Django — and what belongs in a template?
What does {{ article.0 }} do in a Django template, and why is it allowed when it's invalid Python?
Article.objects.get(title='News') works in the shell, but in production it sometimes throws and sometimes works. What two exceptions must you handle, and when does each fire?
In a Django field lookup like Comment.objects.filter(article__id=2), what does the double underscore __ mean?
Printing a Django model instance shows <Article: Article object (2)> instead of the title. What did you forget, and where does Django use it?
After saving a POST'd comment, the view returns render(...) for the detail page. The user hits refresh and the comment gets saved AGAIN. What's the correct pattern?
A Django POST form submits but Django rejects it with a 403 (CSRF verification failed). What's missing from the template, and why does Django require it?
What does "batteries-included" mean for Django, and how does that contrast with a microframework like Flask?
How do you get an auto-generated CRUD admin UI for a Django model, and what one-time setup does the admin need?
What command starts Django's built-in web server, and what's the one rule you must never break with it?
Which Django command-line entry point do you use for project-level tasks once a project exists, and how does it differ from django-admin?
Django is built on two design principles: 'loose coupling' and DRY. What practical payoff does each give you?
Loading a template, rendering a context, and wrapping it in HttpResponse is three lines in every view. What single Django call replaces all three?
Where does Django look for a template file by default, and why nest it in a per-app subfolder like templates/news/?
A Django template has {{ article.comment_set.count }} — count is a method, yet there are no parentheses. Why does it still work?
In a Django template, how do you HTML-escape a value and then convert its newlines to <br/>, in one expression?
Instead of hard-coding /articles/2/ in a template link, how do you build the URL from the named rule, passing the article id?
Many Django pages share the same header/footer. How do you define that shell once and let child pages fill in just the middle?
Django form data: what's the visible difference between GET and POST transfer, and how does a view read a submitted field?
What does Django's admin record automatically for every edit to a record, and why is that valuable with multiple staff users?
A Comment references one Article (many comments per article). How do you declare that, and what does the required on_delete=models.CASCADE do?
ForeignKey gives a one-to-many link. What other two relationship kinds does Django support between models?
In the Django shell, how do you insert a new row for a model, and where does the row's id come from?
You have an Article instance whose Comment model has a ForeignKey to Article. How do you add and count that article's comments without touching Comment.objects?
What does Article.objects.all() return, and when you write objects.filter(a=1, b=2), how are the two conditions combined?
In a Django filter, how do you express 'id is in a list', 'title starts with X', and a case-insensitive version of any lookup?
A Django view builds plain text but the browser mangles the display. How do you set the response's content type, and with what syntax?
In a project's root urls.py, what does include('news.urls') do to the incoming address, and why does that let the app use relative paths?
Inside a Django view, how do you send a 'page not found' to the browser, and where is it typically raised?
What does get_object_or_404(Article, id=x) do that a bare Article.objects.get(id=x) doesn't, and what must its first argument be?
In Django admin, comments show as a separate flat list instead of being edited on their parent article's page. How do you embed them inline?
django-admin startproject news_site makes two nested news_site folders. What's the split, and what do settings.py, urls.py, and wsgi.py/asgi.py each do?
What do WSGI and ASGI standardize in Python web deployment, and how are the two related?
In Django's settings.py, what is the shape of the DATABASES setting, which entry must always exist, and how do SQLite vs server-database configs differ?
Match each Django database ENGINE string to its backend, and note which need a third-party package: sqlite3, mysql, postgresql, oracle.
You need three model fields: a title (short), a body of arbitrary length, and a publish timestamp. Which Django field type fits each?
Start learning today
Free to start — download the app or use it in your browser.
