Django Flashcards

1
Q

Creating a new project

A

in the desired directory:

django-admin.py startproject mysite

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

syncdb

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

create an app

A

python manage.py startapp app_name

creates a directory with structure, for one app

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

after adding an app to the settings file’s INSTALLED_APPS

A

python manage.py sql polls
this shows the sql, but doesn’t actually run it;
syncdb does

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what must be called explicitly after creating a new object in a django table

A

variable.save()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what must be added to models?

A

__unicode__ method, for sanity and because those reps are used throughout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is exactly the same as:

Poll.objects.get(id=1)

A

Poll.objects.get(pk=1)

This is because primary keys are used so often

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to register an app on the auto-generated admin menu

A
in admin.py, import the module and register it with:
admin.site.register(module)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

to change the way fields are ordered in the admin menu

A
class PollAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question']

admin.site.register(Poll, PollAdmin)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
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?]

A

This tells Django: “Choice objects are edited on the Poll admin page. By default, provide enough fields for 3 choices.”

[TabularInline]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what to put in settings to get your template directory looked at?

A

TEMPLATE_DIRS = [os.path.join(BASE_DIR, ‘templates’)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

simplest view possible in Django

A

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what four arguments are passed into url() in a URLconf[iguration]

A

required: regex and view
optional: kwargs and name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Syntax: dev server

A

python manage.py runserver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does every single view need to return?

A

an HttpResponse or an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What arguments does django.shortcuts.render() take?

A

(a request object, template name, [optional dictionary])