Django Intermediate Flashcards

1
Q

What are Django Signals

A

Whenever there is a modification in a model, we may need to trigger some actions.
Django provides an elegant way to handle these in the form of signals. The signals are the utilities that allow us to associate events with actions. We can implement these by developing a function that will run when a signal calls it

django. db.models.pre_init &
django. db.models.post_init Sent before or after a models’s init() method is called
django. db.models.signals.pre_save & django.db.models.signals.post_save Sent before or after a model’s save() method is called
django. db.models.signals.pre_delete &
django. db.models.signals.post_delete Sent before or after a models’ delete() method or queryset delete() method is called
django. db.models.signals.m2m_changed Sent when a ManyToManyField is changed
django. core.signals.request_started &
django. core.signals.request_finished Sent when an HTTP request is started or finished

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

Explain the caching strategies in the Django

A

Caching refers to the technique of storing the output results when they are processed initially so that next time when the same results are fetched again, instead of processing again those already stored results can be used, which leads to faster accessing as well us less resource utilization. Django provides us with a robust cache system that is able to store dynamic web pages so that these pages don’t need to be evaluated again for each request.

Some of the caching strategies in Django are listed below:

Memcached : A memory-based cache server is the fastest and most efficient
FileSystem Caching : Values of the cache are stored as separate files in a serialized order
Local-memory Caching : This is used as the default cache strategy by Django if you haven’t set anything. It is per-process as well as thread-safe.
Database Caching : Cache data will be stored in the database and works very well if you have a fast and well-indexed DB server.

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

Explain user authentication in Django

A

Django comes with a built-in user authentication system, which handles objects like users, groups, user-permissions, and few cookie-based user sessions. Django User authentication not only authenticates the user but also authorizes him.

The system consists and operates on these objects:

    Users
    Permissions
    Groups
    Password Hashing System
    Forms Validation
    A pluggable backend system
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to configure static files

A

Ensure that django.contrib.staticfiles is added to your INSTALLED_APPS

In your settings file. define STATIC_URL for ex.

STATIC_URL = ‘/static/’

In your Django templates, use the static template tag to create the URL for the given relative path using the configured STATICFILES_STORAGE.

{% load static %}
<img></img>

Store your static files in a folder called static in your app. For example my_sample/static/my_sample/abcxy.jpg

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

Explain Django Response lifecycle

A

Whenever a request is made to a web page, Django creates an HttpRequest object that contains metadata about the request. After that Django loads the particular view, passing the HttpRequest as the first argument to the view function. Each view will be returning an HttpResponse object.
On the big picture following steps occur when a request is received by Django:

First of the Django settings.py file is loaded which also contain various middleware classes (MIDDLEWARES)
The middlewares are also executed in the order in which they are mentioned in the MIDDLEWAREST
From here on the request is now moved to the URL Router, who simply gets the URL path from the request and tries to map with our given URL paths in the urls.py. 
As soon as it has mapped, it will call the equivalent view function, from where an equivalent response is generated
The response also passes through the response middlewares and send back to the client/browser
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What databases are supported by Django

A

PostgreSQL and MySQL, SQLite and Oracle. Apart from these, Django also supports databases such as ODBC, Microsoft SQL Server, IBM DB2, SAP SQL Anywhere, and Firebird using third-party packages. Note: Officially Django doesn’t support any no-SQL databases

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

What’s the use of a session framework

A

Using the session framework, you can easily store and retrieve arbitrary data based on the pre-site-visitors. It stores data on the server-side and takes care of the process of sending and receiving cookies. These cookies just consist of a session ID, not the actual data itself unless you explicitly use a cookie-based backend

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

What’s the use of Middleware in Django

A

Middleware is something that executes between the request and response. In simple words, you can say it acts as a bridge between the request and response. Similarly In Django when a request is made it moves through middlewares to views and data is passed through middleware as a response

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

What is context in the Django

A

Context is a dictionary mapping template variable name given to Python objects in Django. This is the general name, but you can give any other name of your choice if you want

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

What is django.shortcuts.render function

A

When a view function returns a webpage as HttpResponse instead of a simple string, we use render(). Render function is a shortcut function that lets the developer easily pass the data dictionary with the template. This function then combines the template with a data dictionary via templating engine. Finally, this render() returns as HttpResponse with the rendered text, which is the data returned by models. Thus, Django render() bypasses most of the developer’s work and lets him use different template engines.
The basic syntax:
render(request, template_name, context=None, content_type=None, status=None, using=None)
The request is the parameter that generates the response. The template name is the HTML template used, whereas the context is a dict of the data passed on the page from the python. You can also specify the content type, the status of the data you passed, and the render you are returning.

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

What’s the significance of the settings.py file

A

As the name suggests this file stores the configurations or settings of our Django project, like database configuration, backend engines, middlewares, installed applications, main URL configurations, static file addresses, templating engines, main URL configurations, security keys, allowed hosts, and much more

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

How to view all items in the Model

A

ModelName.objects.all()

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

How to filter items in the Model

A

ModelName.objects.filter(field_name=”term”)

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