Django Flashcards
what is context variable lookup?
it’s a mechanism in django that allows us to insert variables in our templates. These variables are added to the context and rendered in the view.
How does DTL handle invalid variables?
returns an empty string.
what is reverse URL resolution in django?
it’s a way to get the absolute url, we can use either use the reverse( ) method or in the template url template tag, and in the model class get_absolute_url( ).
What is the functionality of django.contrib.auth app?
It’s a built-in Django application. It gives authentication and authorization functionality out of the box.
describe laziness in django.
The concept of laziness refers to when a function call is not immediately executed until its return value is accessed. The best example of this in Django is the querysets. The queryset object doesn’t actually make a call to the database until the queryset is evaluated. Laziness improves performance in our Django applications because we take fewer trips to the DB.
how does Django deal with static file optimization?
Django makes use of the ManifestStaticFileStorage to optimize static files. it does this by taking advantage of browser caching. when we collectstatic it appends an md5 hash of the contents of each static to its name. You can cache this file. on the browser it’s one less asset to load, we can also cache this for pages that still make reference to it.
What is ManifestStaticFileStorage and how does it work?
It is one of the ways Django optimizes static files. it stores the file name it handles by appending the md5 hash of the files content to the filename. ex css/styles.css would also be saved as css/55e7cbb9a48.css
what is the STATIC_ROOT setting in Django?
it’s the absolute path to the directory where we collectstatic files to be served.
What is the STATIC_URL setting in django?
It’s the url to use when referring to static files located in the static root.
What is the purpose of the ManifestStorageFile class?
It allows developers to serve old static files by pages that still reference them. it also helps with load time by taking advantage of the browser caching.
what is collectstatic?
collectstatic scans the directories in the setting STATICFILES_DIRS. All static files found are then
collected to the directory at STATIC_ROOT.
what is pagination?
Pagination is a way to split a large data set into smaller data subsets that you can iterate over the pages/subsets. The pages are provided as per user request. Advantage of pagination is improved performance, less bandwith and overhead for each request.
How do you implement pagination in django?
Use the Paginator class or module.
what are signals in django?
Signals is a mechanism in django that allows developers to communicate events between apps. There are many types of signals in django. The ones i am familiar with are the model based signals. ex pre_save, post_save, pre_delete etc.
What are some django signals?
Model signals: pre_save, post_sav, pre_delete.
What is the advantage of signals?
We use signals to transmit messages between applications. The messages will give notification of an event. Signals are useful when several applications in your project are interested in the same mutual event.
What is the ORM
object-relational mapping, The ORM maps your model attributes to your relational database table fields. it also provides a convenient API to communicate with your DB using a python object so developers don’t need to write any SQL.
What are some advantages of Django’s database migrations?
1) provides versioning for your database schemas.
2) keeps your database and models in sync.
What is the difference between Django.TestCase and unittest.TestCase?
Django.TestCase is a subclass of unittest.TestCase. The TestCase instance encloses all operations into a database transaction that it rolls back at the end. This is how it does to keep the database clean for the following tests.
What is the difference between setUpTestData and set up?
You should use setUpTestData for the shared data among your tests as it runs once per class and rolls back all transactions at the end of execution. setUp runs once per test useful if you need to run an operation once per test.
What are some advantages of mixins?
The primary advantage is code reusability.
Describe Mixins
Mixins make use of pythons multiple inheritance and extends functionality for classed based views. It allows you to combine attributes and methods of multiple parents through the use of multiple inheritance.
What are some disadvantages of mixins?
It can lead to issues with code readability if the mixins have deep inheritance trees.
What are some differences between class-based views and function-based views?
1) in classed based views the code is organized around defining specific HTTP methods or providing generic view classes that do and extending functionality by either providing mixins or overriding methods and attributes. 2) function-based views, on the other hand, are organized using generic python functions that use conditional branching and renders a response. 3) Class-based views are python objects and function-based vies are regular python views. 4) Class-based views provide better reusability with the use of multiple inheritance and mixins. One isn’t better than the other it just depends on your problem.