Django 2 Flashcards

1
Q

Which command lets you start a Django project?

A

django-admin startproject mysite

where mysite is the name of the directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What arborescence does the startproject command create?

A

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is the outer mysite/?

A

The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is manage.py?

A

manage.py is a command-line utility that lets you interact with this Django project in various ways.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is the inner mysite/?

A

The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is mysite/__init__.py?

A

mysite/__init__.py is an empty file that tells Python that this directory should be considered a Python package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is mysite/settings.py?

A

mysite/settings.py is the settings/configuration for this Django project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is mysite/urls.py?

A

mysite/urls.py is the URL declarations for this Django project; a “table of contents” of your Django-powered site.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

What is mysite/wsgi.py?

A

mysite/wsgi.py is an entry-point for WSGI-compatible web servers to serve your project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which command lets you start the development server?

A

python manage.py runserver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you specify a custom port when starting the development server?

A

python manage.py runserver 8080

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does automatic reloading work with runserver?

A

The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which command lets you start an app with Django?

A

python manage.py startapp appname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which command lets you create a virtual environement called myenv?

A

virtualenv myenv

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which command lets you activate a virtual environment?

A

source my_env/bin/activate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which command lets you deactivate an environment?

A

source myenv/bin/deactivate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Where do packages get installed in your virtual environment?

A

myenv/lib/python3.7/site-packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which django function lets you check your Django version?

A

django.get_version()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you create create the tables in the database required by the applications listed in INSTALLED_APPS?

A

python mysite/manage.py migrate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What happens when you apply migrations?

A

By applying migrations, the tables for the initial applications are created in the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Is Django’s server suitable for production use?

A

No, runserver is suitable for development. In order to deploy Django in a production environment, you should run it as a WSGI application using a real web server, such as Apache, Gunicorn, or uWSGI.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

In settings.py, what is the DEBUG setting?

A

DEBUG is a boolean that turns the debug mode of the project on and off. If it is set to True, Django will display detailed error pages when an uncaught exception is thrown by your application. When you move to a production environment, remember that you have to set it to False. Never deploy a site into production with DEBUG turned on because you will expose sensitive project-related data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

In settings.py, what is ALLOWED_HOSTS?

A

ALLOWED_HOSTS is not applied while debug mode is on, or when the tests are run. Once you move your site to production and set DEBUG to False, you will have to add your domain/host to this setting in order to allow it to serve your Django site.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

In settings.py, what is INSTALLED_APPS?

A

INSTALLED_APPS is a setting you will have to edit for all projects. This setting tells Django which applications are active for this site.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the default installed app django.contrib.admin?

A

django.contrib.admin is an administration site.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the default installed app django.contrib.auth?

A

django.contrib.auth is an authentication framework.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the default installed app django.contrib.contenttypes?

A

django.contrib.contenttypes is a framework for handling content types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the default installed app django.contrib.sessions?

A

django.contrib.sessions is a session framework.

30
Q

What is the default installed app django.contrib.messages?

A

django.contrib.messages is a messaging framework.

31
Q

What is the default installed app django.contrib.staticfiles?

A

django.contrib.staticfiles is a framework for managing static files.

32
Q

In settings.py, what is MIDDLEWARE?

A

MIDDLEWARE is a list that contains middleware to be executed.

33
Q

In settings.py, what is ROOT_URLCONF?

A

ROOT_URLCONF indicates the Python module where the root URL patterns of your application are defined.

34
Q

In settings.py, what is DATABASES?

A

DATABASES is a dictionary that contains the settings for all the databases to be used in the project. There must always be a default database. The default configuration uses an SQLite3 database.

35
Q

In settings.py, what is LANGUAGE_CODE?

A

LANGUAGE_CODE defines the default language code for this Django site.

36
Q

In settings.py, what is USE_TZ?

A

USE_TZ tells Django to activate/deactivate timezone support. Django comes with support for timezone-aware datetime. This setting is set to True when you create a new project using the startproject management command.

37
Q

In Django, what is a project?

A

A project is considered a Django installation with some settings.

38
Q

In Django, what is an application?

A

An application is a group of models, views, templates, and URLs. Applications interact with the framework to provide some specific functionalities and may be reused in various projects. You can think of the project as your website, which contains several applications such as a blog, wiki, or forum, that can be used by other projects also.

39
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is admin.py?

A

admin.py is where you register models to include them in the Django administration site—using the Django admin site is optional.

40
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is apps.py?

A

apps.py include the main configuration of the blog application.

41
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is migrations/?

A

The migrations/ directory will contain database migrations of your application. Migrations allow Django to track your model changes and synchronize the database accordingly.

42
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is migrations/?

A

models.py holds the data models of your application—all Django applications need to have a models.py file, but this file can be left empty.

43
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is tests.py?

A

tests.py is where you can add tests for your application.

44
Q

blog/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

In the basic structure of an application, what is tests.py?

A

view.py is where the logic of your application goes. Each view receives an HTTP request, processes it, and returns a response.

45
Q

What is a Django model?

A

A model is a Python class that subclasses django.db.models.Model, in which each attribute represents a database field. Django will create a table for each model defined in the models.py file. When you create a model, Django provides you with a practical API to query objects in the database easily.

46
Q

What does the unique_for_date parameter does in models.SlugField()?

A

unique_for_date lets us build URLs for posts using the publish date and slug. Django will prevent multiple posts from having the same slug for a given date.

47
Q

How do you activate your application?

A

Add the app from blog/apps.py to the list of INSTALLED_APPS in mysite/settings.py, for example blog.apps.BlogConfig.

48
Q

Why do you need to activate your application?

A

You need to activate your application to let Django know that our application is active for this project so that it is able to load the application’s models.

49
Q

What does the migrate command do?

A

The migrate command applies migrations for all applications listed in INSTALLED_APPS; it synchronizes the database with the current models and existing migrations.

50
Q

Which command lets you create an initial migration for your model?

A

python manage.py makemigrations blog

51
Q

Which command you inspect the SQL output of a migration?

A

python manage.py sqlmigrate blog 0001

where 0001 is the migration number.

52
Q

Which command lets you apply migrations?

A

python manage.py migrate

53
Q

What happens after applyting migrations for the applications listed in INSTALLED_APPS?

A

After applying migrations, the database reflects the current status of our models.

54
Q

What do you have to do after adding new models, or editing existing ones to add, remove or change fields?

A

After adding or existing models, you need to create a new migration using the makemigrations command to allow Django to keep track of model changes. Then, you will have to apply it with the migrate command to keep the database in sync with your models.

55
Q

Which command lets you create a user to manage the administration site?

A

python manage.py create superuser

56
Q

How do you add models to the administration site?

A

You need to edit your application’s admin.py to import and register the models.

57
Q

Which function lets you retrieve a single object from the database?

A

Object.objects.get(username=’admin’) to retrieve the Admin user, for example.

58
Q

Which function lets you retrieve all objects in the database?

A

Object.objects.all()

59
Q

On what is the Django object-relational mapping (ORM) based?

A

Django’s ORM is based on QuerySets.

60
Q

How does QuerySet evaluation work?

A

Django QuerySets are lazy; they are only evaluated when they are forced to.

61
Q

Which function lets you filter QuerySets?

A

Object.objects.filter()

62
Q

Which function lets you exclude results from your QuerySets?

A

Object.objects.exlude()

63
Q

Which function lets you exclude results from your QuerySets?

A

Object.objects.order_by()

64
Q

Which function lets you delete objects using QuerySets?

A

Object.objects.get(id=1).delete(), for example.

65
Q

When are QuerySets evaluated?

A
  • The first time you iterate over them
  • When you slice them, for instance, Post.objects.all()[:3]
  • When you pickle or cache them
  • When you call repr() or len() on them
  • When you explicitly call list() on them
  • When you test them in a statement, such as bool(), or , and, or if
66
Q

What is a QuerySet?

A

A QuerySet is a collection of SQL queries.

67
Q

What are data migrations?

A

Data migrations are a way to alter database schema.

68
Q

What is the workflow for data migration?

A
  • Create or modify models.py
  • Create migration file
  • Commit changes to the database using the migration file
69
Q

What is a manager?

A

A manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.

70
Q

What is a model?

A

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each attribute of the model represents a database field.

71
Q

When defining a custom manager, which methid do you need to override to to include your custom filter in the final QuerySet?

A

get_queryset(), as it returns the QuerySet that will be executed.

72
Q
A