Writing your first Django app v. 1.8
manage.py
A command-line utility that lets you interact with this Django project in various ways
Object-Relational Mapper (ORM)
An abstraction layer for data in a database
mysite/__init__.py
An empty file that tells Python that this directory should be considered a Python package.
project
Another name for a collection of apps
models
Another name for database layout
self.assertEqual(response.status_code, 302)
Asser test for a HttpRedirect
self.assertContains(response, 'item 1')
Assert function that can check an entire response without needing to decode the html
assertTemplateUsed
Assert function to test templated used
assertContains()
Assert method used in testing to check whether something contains something
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py
Basic directory layout of Django program mysite
LiveServerTestCase
Class to use for functional tests to build and tear down the database each test
{% csrf_token %}
Code for cross site request forgerie token
forloop.counter
Code snippet that tells django the iteration of a for loop
python manage.py runserver
Command that begans the python development server
django-admin startproject mysite
Command that creates project "mysite" in working directory
python manage.py migrate
Command that looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app
python manage.py migrate
Command that takes all the migrations that haven't been applied (Django tracks which ones are applied using a special table in your database called django_migrations) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.
python manage.py createsuperuser
Command to create a superuser
python manage.py startapp polls
Command to create the app polls
class Choice(models.Model): question = models.ForeignKey(Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
Create a choice model that has a ForeignKey as Question, choice_text (char) and votes (Integer)
class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
Create a question model in django with a question_text variable set to a charField and a pub_date variable set to a DateTimeField
item_one = Item()
Create an Item object in django's test file
request.POST
Dictionary like object that lets you access submitted data by a key name
polls/ __init__.py admin.py migrations/ __init__.py models.py tests.py views.py
Directory layout of the app polls
mysite/mysite/
Directory that contains the python package for the program
{% for .. in .. %}
Django template syntax for iterating through a list
def __str__(self): / return self.choice_text
Function for each class in models that displays the human readable text of the class
render()
Function in views that takes a request object as its first argument, a template name as its second argument and a dictionary as its optional third argument. It returns an HttpResponse object of the given template rendered with the given context.
include()
Function that allows for referencing otherURLs conf. Django chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
.decode()
Function that converts bytes into python strings
get_object_or_404()
Function that takes a Django model as its first argument and an arbitrary number of keyword arguments, which it passes to the get() function of the model's manager. It raises Http404 if the object doesn't exist.
HttpResponseRedirect()
Function where the argument takes a single argument: the URL to which the user will be redirected. You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn't specific to Django; it's just good Web development practice.
unittest.TestCase
Functional test cases inherit from this class in Django
python -c "import django; print(django.get_version())"
Get django version from the interpreter
HttpRequest().method
HttpRequest method that returns the type of method being used (e.g. POST, GET)
from . import views
Import statement that is needed for urls.py to map the urls to the views
admin.site.register(class)
Line to insert into admin.py that registers the model with the admin site
python manage.py makemigrations polls
Method that tells Django that you've made some changes to your models (in this case, you've made new ones) and that you'd like the changes to be stored as a migration.
MVC
Model View Controller pattern
from django.conf.urls import url
Module that needs to be imported into urls.py
from django.http import HttpResponse
Module that needs to be imported to views.py
mysite/settings.py
Pathway of Settings/configuration for this Django project.
mysite/urls.py
Pathway of the URL declarations for this Django project; a "table of contents" of your Django-powered site
mysite/wsgi.py
Pathway of website gateway interface for the project
render_to_string( 'home.html', {'new_item_text': 'A new list item'}
Python function that render a HttpResponse to a string
Item.objects.all()
Retrieve all items from a table in django's testbench
item_one.text
Retrieve item_one's text attribute using django's test file
item_one.save()
Save item_one in djangos test file
.objects.create
Shorthand in views file to create a new object without having to call save()
{% load staticfiles %}
Tag to be included in html that loads static files
{% url %}
Template tag that can be used if you define the "name" paramater in the urls.py file
urls(regex, view, kwargs, name)
The arguments for the keyword function
django.db.models.Model
The django class on which models are built
INSTALLED_APPS
Tuple or dict in settings that holds the names of all Djanog applications that are activated in this Django instance
URLconfs
What django uses to map urls to their respective views
view
a "type" of Web page in your Django application that generally serves a specific function and has a specific template
app
another name for a web application that does something
integrated test
test type that relies on an external system (e.g. a database)