Overview Flashcards
How does Django map HTML requests to Views?
Via the urls.py file from the project package. The urls.py has a list of Regular Expression patterns that map to Views. Optionally, this mapping can also have a name.
urlpatterns = patterns(‘’,
url(r’^$’, ‘story.views.home’, name=’home’),
)
What does django.conf.urls.include() do?
include() is used within the urlpatterns variable in your urls.py to import another App’s urls into your Project’s urls.
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
What is django.conf.urls.url() for?
What are its parameters?
Used within the urlpatterns list within a urls.py file. urlpatterns should be a list of url() instances.
url(regex, view, kwargs=None, name=None)
The view parameter is a view function or the result of as_view() for class-based views. It can also be an include(). Django calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the regular expression as other arguments.
The kwargs parameter allows you to pass additional arguments to the view function or method.
The name parameter allows you to dynamically build URLs in your templates based off of the URL’s name.
When you update your Models, what manage.py commands need run to update your database?
python manage.py makemigrations
( Creates the migration files, which can then be checked into SCM and run with migrate
)
and
python manage.py migrate
( Runs all available migration files that haven’t been run yet (which is tracked by the django_migrations table in your database))
What command has Django look over your project for any common errors?
python manage.py check
What command allows you to preview any SQL that is run during a migration and what are its arguments?
python manage.py sqlmigrate APP MIGRATION_FILE
Why is adding a __str__() method to your Models important?
The __str__() representation of your Models is used in the manage.py shell and also in multiple places in the Django Admin dashboard.
How do you pass pieces of the URL to your view methods?
You can use Capture Groups (named or not) within the Regex in your URLs to send information to the view methods.
The view methods then receive all captured fields as parameters and named capture fields as keyword arguments.
Example:
url(r’^(?P[0-9]+)/results/$’, views.results, name=’results’)
Will match on requests like:
my-domain.com/34/results/
And call the view with:
results(request=, question_id=’34’)
Where does Django look for templates by default?
Your project’s TEMPLATES setting describes how Django will load and render templates. The default settings file configures a DjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.
What does it mean to “namespace your templates” and why is that important?
Django will load all template directories onto the same level. So all templates loaded from various app’s /templates directory are brought side-by-side, easily creating a potentially conflicting situation for common names.
To solve this, it is suggested to create another folder within APP/templates/ and place your templates within there. For example, the full path to a particular app’s templates may look something like:
/my-project/my-app/templates/my-app/index.html
And that template would be referred to by the name “my-app/index.html”