MVC & MTV Frameworks Flashcards

1
Q

What are routes?

A

Routing is handled by a separate document that contains a list of available routes and calls to the appropriate view functions. When a request is received routing identifies the view method to be called.

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

What are view functions?

A

view functions generally:

Redirect to other routes
Render specific templates
Invoke methods attached to other pieces of our app that we characterize as models

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

What are models?

A

It is likely that the views file will call some model method in order to perform some operation on the database. Models should do the following:

Build database tables
Handle logic related to database operations, including validation

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

What are templates?

A

a view function will either render a template or redirect. When a template is rendered Django has to parse the file looking for logic or variables you’ve inserted, interpret those, before sending a complete HTML document as a response to the client. Django templates are so similar to Flask (Jinja2) templates that you’ll hardly notice a difference.

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

What does MTV stand for?

A

MTV stands for ‘model template view’, which describes the division of labor in Django.

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

What does MVC stand for?

A

Models, Views and Controller
although Django’s terminology varies slightly from the usual MVC pattern, the two are close enough that Django may be considered an MVC framework. Although it is important to use the right term for each component, we can think of Django as an MVC framework.

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

What does Controller mean?

A

The MVC controller is roughly equivalent to Django’s views. In both framework types this component should be in charge of outsourcing work to the models and sending the appropriately formatted data to the client. We mentioned that some other frameworks don’t outsource logic to the model. In those cases, many of our logical operations are done in the controller.

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

Define Object-Relational-Mapper (ORM)

A

Django uses an Object-Relational-Mapper (ORM) to map Model definitions in the Django code to the data structure used by the underlying database. As we change our model definitions, Django tracks the changes and can create database migration scripts (in /locallibrary/catalog/migrations/) to automatically migrate the underlying data structure in the database to match the model.

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

What is settings.py?

A

contains all the website settings. This is where we register any applications we create, the location of our static files, database configuration details, etc.

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

What is urls.py

A

defines the site url-to-view mappings. While this could contain all the url mapping code, it is more common to delegate some of the mapping to particular applications

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

What is wsgi.py

A

py is used to help your Django application communicate with the web server. You can treat this as boilerplate.

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

What is manage.py?

A

The script is used to create applications, work with databases, and start the development web server.

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

What is the migrations folder used for?

A

used to store “migrations” — files that allow you to automatically update your database as you modify your models.

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

What is the __init__.py file?

A

an empty file created here so that Django/Python will recognise the folder as a Python Package and allow you to use its objects within other parts of the project.

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

What is request.POST?

A

Data from POST request

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

What is request.GET?

A

Data from GET request

17
Q

What is request.method?

A

Returns the method/HTTP verb associated with the request

18
Q

what is {% csrf_token %} used for?

A

Prevents cross-site request forgery (place it in a form on the HTML/template side of your project)

19
Q

What is request.session?

A

As request.session is just a dictionary, you can attach any key/value pairs. For example, in the views.py file, you could do:
request.session[‘name’] = request.POST[‘name’]
request.session[‘counter’] = 100
In the html file, Django lets you directly access request.session. For example to access request.session[‘name’], simply do: {{request.session.name}}

20
Q

What does the request.session[‘key’] method do?

A

This will retrieve (get) the value stored in key

21
Q

What does the request.session[‘key’] = ‘value’ do?

A

Set the value that will be stored by key

22
Q

What does del request.session[‘key’] do?

A

Deletes a session key if it exists, throws a keyError if it doesn’t. Use along with try and except since it’s better to ask for forgiveness than permission

23
Q

What does ‘key’ in request.session do?

A

Returns a boolean of whether a key is in session or not

24
Q

What does {{ request.session.name }} do?

A

Use dot notation (.) to access request.session keys from templates since square brackets ([]) aren’t allowed there