Django Level Four Flashcards
what is the syntax for the best method of injecting a url template into an html file?
what is required in the urls.py file that wasn’t before?
”{% url “basic_app:other” %}”
app_name = name_of_app should be in the apps urls.py file before the mapped url patterns.
How do you use url template tagging to link to the admin site?
set the href equal to:
”{% url “admin:index” %}”
what is the syntax for linking a url coming straight from the projects urls.py file?
”{% url “index” %}”
it is this simple since we do not need to identify an app.
syntax for both the base.html and the html file that inherits from it.
it is good practice to place the block template tags of the base.html inside of a …
div tag with class=”container”
the second line of any html file that extends another needs to contain what?
a template tag that identifies the file being extended.
{% extends “basic_app/base.html” %}
To use custom filters in an html file, one must add this to the top of the html file.
{% load name_of_filters_file %}
What is the general form for a template filter?
{{ value | filter: “arg” }}
What should you do before trying to build your own template filter?
See if django already has one built in.
3 steps to creating a custom filters file.
- create a directory called template tags on the same level as models.py
- create a file called __init__.py so that python treats the directory as a module
- create a file inside the directory that can be called anything. this file will contain related filter tags
What must be imported into the template filters file? what class do we instantiate from it? and for what purpose?
from django import template
register = template.Library()
To use as a decorator to register the function into a filter tag
when registering a new filter tag, what arg do we pass into the decorator above the function?
@register.filter(name=’cut’)
the name should be the same as the functions name.
What should be the first thing found in a function after the definition?
a docstring explaining what it does and concisely as possible.