Django Flashcards
Creating a new project
in the desired directory:
django-admin.py startproject mysite
syncdb
inside a new project:
python manage.py syncdb
The syncdb command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file.
create an app
python manage.py startapp app_name
creates a directory with structure, for one app
after adding an app to the settings file’s INSTALLED_APPS
python manage.py sql polls
this shows the sql, but doesn’t actually run it;
syncdb does
what must be called explicitly after creating a new object in a django table
variable.save()
what must be added to models?
__unicode__ method, for sanity and because those reps are used throughout
what is exactly the same as:
Poll.objects.get(id=1)
Poll.objects.get(pk=1)
This is because primary keys are used so often
how to register an app on the auto-generated admin menu
in admin.py, import the module and register it with: admin.site.register(module)
to change the way fields are ordered in the admin menu
class PollAdmin(admin.ModelAdmin): fields = ['pub_date', 'question']
admin.site.register(Poll, PollAdmin)
what does this mean: class ChoiceInline(admin.StackedInline): model = Choice extra = 3
class PollAdmin(admin.ModelAdmin) yadda inlines = [ChoiceInline]
[bonus points: what’s a sexier way than StackedInline?]
This tells Django: “Choice objects are edited on the Poll admin page. By default, provide enough fields for 3 choices.”
[TabularInline]
what to put in settings to get your template directory looked at?
TEMPLATE_DIRS = [os.path.join(BASE_DIR, ‘templates’)]
simplest view possible in Django
from django.http import HttpResponse
def index(request): return HttpResponse("Hello")
what four arguments are passed into url() in a URLconf[iguration]
required: regex and view
optional: kwargs and name
Syntax: dev server
python manage.py runserver
What does every single view need to return?
an HttpResponse or an exception