Django Flashcards
What is Models?
Deals with data and databases, it can retrieve, store, and change data in a database.
What is Templates?
Determines how the data looks on a web page.
What is Views?
Describes the data to be presented, and passes this information to the template.
How to start Django project?
django-admin <command></command> [options]
django-admin startproject projectname
What are apps in Django?
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
What are projects in Django?
A Django project refers to the entire code base and its parts.
What is the folder structure for a Django app?
projectname/
|– appname/
|– templates/
|– appname/
|– file.html
|– static/
|– appname/
|– file.css
What is DTL?
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.
What are some drawbacks to preinstalled SQLite?
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.
What is the difference between null and blank?
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.
What are methods in models?
Methods are functions defined in our model that describe the behaviors and actions of our model.
What are migrations? When is it necessary? How do you make migrations?
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.
- Running python3 manage.py makemigrations to create a file with the instructions needed for our database to create the proper schemas.
- Running python3 manage.py migrate to execute the instructions in our file to create the actual tables in our database.
How to reverse a migration?
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 to get data from database using Django shell?
ModelName.objects.get(name=”name”)
How to delete data from database using Django shell?
ModelName.objects.first().delete()