• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/67

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

67 Cards in this Set

  • Front
  • Back
how do you create a new django project?
django-admin.py startproject project_name
where should django code not go?
in the web server's document root for security reasons
what does __init__.py do in the django project?
it tells python the directory should be a python package
what is manage.py in a django project?
a command-line utility for interacting with the project
allowing you to do things like run the server and view sync the database etc
what is urls.py in a django project?
it contains a python module that directs requests for urls to the appropriate python code
how do you start the development server in django?
python manage.py runserver from the project root directory
how do you start the django development server with a different port or ip address?
python manage.py runserver [new port]

python manage.py runserver [new-ip-address]
where do you set the database in a django project?
in the settings.py file change the DATABASES variable
where do you include different django applications into your django project?
in the settings.py file add apps to the INSTALLED_APPS variable
how do you create tables in the database based on the django applications in your django project?
python manage.py syncdb
What's the difference between a project and an app?
An app is a Web application that does something -- e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
how do start a new django app?
python manage.py startapp polls
what are django models?
the definitive source of data about your data (essential fields and behavior), database layout with additional metadata
how do you create a new model in a django app?
modify the models.py file and add a class

class [model_name](models.Model):
declare model fields here
how are fields added to a model?
instances of field classes are created and assigned to variables in the model class
what is the first positional argument to a Field class?
an optional human readable name for the field
how do you create a character field for a django model?
field_name = models.CharField(max_length=[max length)
how do you relate model A to model B where model A is in a many-to-one relationship with model B?
in model A have the field
[relation field name] = models.ForeignKey([B's name])
how do you see the sql for a django app that has been added to settings.py?
python manage.py sql [app name] from the project root directory
how do you check for errors in the construction of django models?
python manage.py validate
how do you view the sql that will be run by 'python manage.py syncdb' for a given app?
python manage.py sqlall [app name]
how do you start a python shell with the django project in the environment?
python manage.py shell from the project root directory
how do you access the models of an app in the django python shell?
from [app name].models import [model name] [model name] ...
how do you make the model representation more readable?
add a __unicode__ method to the model class
how do you find a django model instance with a given id?
[model name].objects.filter(id=[desired id])
how do you find a django model instance with a given primary key?
[model name].objects.get(pk=[primary key])
if there is a model with A with a many-to-one relationship with a model B how do you get all the A instances associated with a B instance?
[b instance name].[A name lowercase]_set.all()
how do you filter model instances based on a character field that starts with a given prefix?
[Model name].objects.filter([field name]__startswith=[prefix])
how do you add the django admin site to a project?
uncomment out the admin app in the settings.py file

run python manage.py syncdb

uncomment out the admin lines in urls.py (two for importing and one in urlpatterns)
how do you make a django app available to the admin interface?
add an admin.py file to the app directory and import admin via
from django.contrib import admin
how do you make specific models in an app available to the admin interface?
import the model to the admin.py file in the app and register it

from [app name].models import [model name]

admin.site.register([model name])
if a model A is many-to-one with B how do you make adding A instances available on the B form in the admin interface
create a class in the admin.py file of the app that has the form

class [model name]Inline(admin.StackedInline):
model = [model name]
extra = [default number of instances to add]

and also create a model admin class for B
[model B name]Admin(admin.ModelAdmin):
inline = [[model A name]Inline]
how do you make the inline form more compact for the admin interface?
when creating the model inline class have it inherit from admin.TabularInline rather than admin.StackedInline
how do you change how model instances are listed in the admin interface?
in the admin model class set list_display variable to be a tuple of fields that should be used to represent the object
how do you make it so model instances can be filtered by a particular field in the admin interface?
set the list_filter variable in the model admin class to be a list of the fields that should be filtered by
how do you make it so model instances can be searched for by field in the admin interface?
set the search_fields to be a list of fields that will be searched in the model admin class
how do you add hierarchical navigation of model instances in the admin interface?
set date_hierarchy = [datetime field] in the model admin class
how do you customize the presentation of the admin interface?
add a directory for templates in settings.py TEMPLATE_DIRS variable

add the directory in the filesystem

add an admin subdirectory and put a modified base_site.html file in there
what template is used if TEMPLATE_DIRS is not specified in the settings.py file?
a templates directory is searched for in individual apps
how do you modify the presentation of index of apps in the admin interface?
copy and modify admin/index.html from the default installation
what is a django view?
A view is a “type” of Web page in your Django application that generally serves a specific function and has a specific template
what are URLConfs?
python modules that tell how python code is attached to urls
how does django direct a users request for a page?
the ROOT_URLCONF lists where the URLConf module is

the URLConf has a urlpatterns variable which is used to match the url requested to a python call-back function
what happens when a match is made in the URLConf urlpatterns for a requested url?
the associated callback function is called with the HTTP request object as the first argument, any "captured" values from the regular expression as keyword arguments, and, optionally, arbitrary keyword arguments from the dictionary (an optional third item in the tuple).
what does the URLConf regular expression not match in the URL?
the domain and the GET/POST parameters i.e. everything past .com/.net etc and before ?
what is expected from a view by django?
an HttpResponse object or an exception
how is a django template used in a view?
load the template

create a context

render the template with the context and return it through the HttpResponse object
how do you load a template for use in a view?
from django.template import loader

loader.get_template('[template path]') #goes inside the view
how do you create a context to be passed to a template in a view?
from django.template import Context

Context({variable name: value, ...}) #goes inside the context
how do you render a template with a given context?
[template object].render([context object]) #returns a string
what is the shortcut for returning a response that is rendered from a template and a context?
from django.shortcuts import render_to_response

render_to_response([path to template], [dictionary that acts as the context])
how do you return a 404 error from a view?
from django.http import Http404

#in the view
try:
normal behavior
except [some exception]:
raise Http404
what is the shortcut for returning a 404 if an object in the database is not found?
from django.shortcuts import get_object_or_404

#in the view
[object variable] = get_object_or_404([model name], pk=[id])
what is the shortcut for returning a 404 if a list of objects in teh database is not found?
get_list_or_404
where does the template for 404 errors go?
in the root of the templates directory for the project
how do you have your django project render custom 404 or 500 errors?
in the root URLconf have a variable called handler404 or handler500 and have its value be the desired callback view
how can you simplify urlpatterns if your call back functions have a common prefix?
the first argument to pattern is a string that prefixes the call back functions
what do you do if you don't want to apply a common prefix to all your call back functions?
create different pattern objects and concatenate them. the call back functions with the same prefix should be in the same pattern object
how do you decouple urls for an app from the root project URLconf?
use an include in the patterns object of the root URLconf
how does include work for URLconfs?
include([regular expression], [app URLConf as a package])

the part of the url that matches the regular expression is cut off and the rest is passed to the app specific URLConf
what should forms in a template that use the post method have to prevent cross site request forgeries?
it should have the
{% csrf_token %}
tag after the open form tag

add context_instance=RequestContext(request) as an argument to the render_to_response call in the view with the form
how do you access form data in a view that was submitted in a post request?
request.POST['[form name]']
what is request.POST?
a dictionary whose keys are form field ids and values are the value for the form field
what object should you return in a view if the request is a form submission to prevent double submissions?
HttpResponseRedirect
how does the url passed to HttpResponseRedirect usually formed?
through the reverse function
how does the reverse function create a url?
it receives the view name in python dot notation along with a variable that is probably an object id then returns the url with the object id followed by the path for the view
what does the generic view ListView do?
it creates a view with a template that takes a list of objects