Django Flashcards

1
Q

What is Models?

A

Deals with data and databases, it can retrieve, store, and change data in a database.

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

What is Templates?

A

Determines how the data looks on a web page.

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

What is Views?

A

Describes the data to be presented, and passes this information to the template.

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

How to start Django project?

A

django-admin <command></command> [options]
django-admin startproject projectname

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

What are apps in Django?

A

a Django app is a submodule to a project, that contains the code for a specific feature.
- To add a new app, run this command in the root folder: python3 manage.py startapp vetoffice
- When adding a new app, you must add it to the INSTALLED_APPS under settings.py

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

What are projects in Django?

A

A Django project refers to the entire code base and its parts.

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

What is the folder structure for a Django app?

A

projectname/
|– appname/
|– templates/
|– appname/
|– file.html
|– static/
|– appname/
|– file.css

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

What is DTL?

A

Django Template Language. Allows you to write django/python expressions within html document. Can be used to display variables, loops, and create conditionals.
You can also use filters to modify the data so that you dont have to write much more code.

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

What are some drawbacks to preinstalled SQLite?

A

SQLite’s signature portability unfortunately makes it a poor choice when many different users are updating the table at the same time (to maintain integrity of data, only one user can write to the file at a time). It also may require some more work to ensure the security of private data due to the same features that make SQLite accessible. Furthermore, SQLite does not offer the same exact functionality as many other database systems, limiting some advanced features other relational database systems offer. Lastly, SQLite does not validate data types. Many other database software would reject data that does not conform to a table’s schema, how a table’s data is organized. However, SQLite allows users to store data of any type into any column.

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

What is the difference between null and blank?

A

null is a field can be left intentionally void of information. blank is similar to null, but setting blank to True means a field doesn’t have to take anything, not even a null value.

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

What are methods in models?

A

Methods are functions defined in our model that describe the behaviors and actions of our model.

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

What are migrations? When is it necessary? How do you make migrations?

A

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.
Migrations are needed when we make changes to our models.

  1. Running python3 manage.py makemigrations to create a file with the instructions needed for our database to create the proper schemas.
  2. Running python3 manage.py migrate to execute the instructions in our file to create the actual tables in our database.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to reverse a migration?

A

python3 manage.py migrate <app_name> <migration_name>
migration_name would be something like 0001 or 0002
Use python3 manage.py showmigrations <app_name> to see a list of all the migrations.</app_name></migration_name></app_name>

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

How to get data from database using Django shell?

A

ModelName.objects.get(name=”name”)

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

How to delete data from database using Django shell?

A

ModelName.objects.first().delete()

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

What is the exclude querying method?

A

Returns objects that do not match the argument

17
Q

What is the order_by querying method?

A

Returns objects in ascending or descending (-) order that match the argument

18
Q

How to query two tables at once?

A

Using the filter() method

19
Q

What does the view do in Django?

A

A view is a function that takes in an HTTP request and returns an HTTP response.

20
Q

What is generic view?

A

They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code.

21
Q

Explain CRUD operations in generic view

A

Import the generic views below:

from django.shortcuts import render
from .models import Owner, Patient
from django.views.generic import ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView

def home(request):
context = {“name”: “Djangoer”}
return render(request, “vetoffice/home.html”, context)

class OwnerList(ListView):
model = Owner
template_name = “vetoffice/owner_list.html”

class PatientList(ListView):
model = Patient
template_name = “vetoffice/patient_list.html”

Create your other generic views below:
class OwnerCreate(CreateView):
model = Owner
template_name = “vetoffice/owner_create_form.html”
fields = [“first_name”, “last_name”, “phone”]

class PatientCreate(CreateView):
model = Patient
template_name = “vetoffice/patient_create_form.html”
fields = [“animal_type”, “breed”, “pet_name”, “age”, “owner”]

class OwnerUpdate(UpdateView):
model = Owner
template_name = “vetoffice/owner_update_form.html”
fields = [“first_name”, “last_name”, “phone”]

class PatientUpdate(UpdateView):
model = Patient
template_name = “vetoffice/patient_update_form.html”
fields = [“animal_type”, “breed”, “pet_name”, “age”, “owner”]

class OwnerDelete(DeleteView):
model = Owner
template_name = “vetoffice/owner_delete_form.html”

class PatientDelete(DeleteView):
model = Patient
template_name = “vetoffice/patient_delete_form.html”

Inside the view.py file, you need to define paths for each crud operations!

22
Q

What does Http404() do?

A

show 404 page if the request object does not exist. Use try, except blocks

23
Q

What is CSRF token?

A

Cross Site Request Forgery protection. The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malicious website contains a link, a form button or some JavaScript that is intended to perform some action on your website, using the credentials of a logged-in user who visits the malicious site in their browser. A related type of attack, ‘login CSRF’, where an attacking site tricks a user’s browser into logging into a site with someone else’s credentials, is also covered.

The CSRF token protects the application and the user by adding a secret token inside of the “POST” methods in the forms each time the form is rendered. The CSRF token ensures that only the proper user is using the proper credentials.

24
Q

What is get_absolute_path method?

A

Redirects user to another page after submitting data.