21
charting django (A map to avoiding that “Where is the bathroom?” “Under the sign.” “Oh.” feeling.) 11/18/2008, 7PM django-nyc Will Larson An intermediate overview of useful features.

Charting Django

  • Upload
    lethain

  • View
    3.505

  • Download
    2

Embed Size (px)

DESCRIPTION

Presentation by Will Larson given at django-nyc on 11/18/2008.

Citation preview

Page 1: Charting Django

charting django(A map to avoiding that

“Where is the bathroom?” “Under the sign.”

“Oh.”feeling.)

11/18/2008, 7PMdjango-nycWill Larson

An intermediate overview of useful features.

Page 2: Charting Django

qualifications

• Spent last year as English teacher.

• Last presentation was to 13 year olds.

• People skills. Damn it.

It’s all true. Damnit.

Page 3: Charting Django

• beginner - templates, views and urls

• intermediate - why you’re listening to me

• advanced - reusable applications

django roadmap

<img src=“funnyPicture.png”>Cats? Kids?</img>

This presentation is geared towards advanced beginners, who want a guide to become intermediate Djangonauts.

I hate that term.

Page 4: Charting Django

django experienceProject Concept Status

GalleryProject gallery builder

LifeFlow blog engine

django-userskins user-specific skins

Everyone needs three projects in their trip towards Django mastery.

Don’t be afraid to start over from scratch. When you make decisions with insufficient knowledge is like trying to get into a building when you don’t know how to open doors.

You’ll probably have to blow a hole in the wall, and even after you learn about the door the hole is still going to be there.

Page 5: Charting Django

• local_settings.py

• fixtures

• template tags & filters

• management commands

• custom context

• middleware

easy to feign mastery

harder to feign mastery

Almost all of this functionality can be ignored, but by using it your applications can be smaller, more flexible, and much easier to write.

Moving from beginner to adept is about increasing efficiency and quality, not necessarily about doing new things, but doing old things better.

Page 6: Charting Django

• some project settings are location specific

• some are not

• Don’t Repeat Yourself

• => local_settings.py

local_settings.pyThis is a common convention, rather than a feature.

It’s even harder to learn convention than to learn features, because they’re often undocumented.

Page 7: Charting Django

settings.py

local_settings.py

Page 8: Charting Django

local_settings.py in VC• versioned:

• settings.py

• local_settings.py.template

• kitchen sink

• not versioned:

• local_settings.py

Page 9: Charting Django

local_settings.py variant• 2 + max(n,1) settings files

• settings.py

• local_settings.py (not in version control!)

• ???_settings.py (deploy, devel, staging, etc)

local_settings.py

This is how I roll.

Bringing settings inheritance to the weary developer/sys admin hybrid.

Page 10: Charting Django

fixtures• export and import data

• extremely poor man’s schema migration

shell session

Can be used for initial and testing data, as well.

I use fixtures to maintain a local copy of my blog , to extract data to run statistics, and--sadly--I do use it for simple migrations.

Page 11: Charting Django

template tags & filters• Don’t Repeat Yourself for templates

• use when justified

You’ve probably used the default templates and filters before. They’re good, but sometimes you run into missing functionality.

Time to roll your own.

Page 12: Charting Django

template filtersmyapp/templatetags/magic.py

some_template.html

Page 13: Charting Django

template tags, 1• more powerful & complex than filters

some_template.html

Kevin twittered about the “pyif” templatetag. That’s a great example of complex and powerful.

That’s because it’s mostly a wrapper around the Python eval() function, i.e. it is basically a templatetag that provides direct access to the Python interpreter.

Model-View-Controller be damned, lets throw the interpreter into the templates.

Page 14: Charting Django

template tags, 2monetize.py

Page 15: Charting Django

management commands• periodic or one-time commands

• great with cron This is your primary interface between the shell and Django, along with:

python manage.py shell

Page 16: Charting Django

clean_revisions.py

shell session

Page 17: Charting Django

custom context• manage global information

• play nicely with generic views

settings.py

These really extend the usability of generic views by letting you inject arbitrary data into all templates (well, all templates served by views that use RequestContext, which includes generic views).

Try to keep them lightweight to avoid slowing down everything.

Page 18: Charting Django

context.py

Page 19: Charting Django

middleware

• four levers to defy Django physics

• process_request

• process_view

• process_response

• process_exception

This is a totally inadequate introduction to middleware, which are one of the more nuanced features of Django.

You could have a 30 minute talk that solely focused on middleware and still have questions left over and rocks left unturned.

This is a sketch of a sketch of a map, but that’s all the time we have.

Page 20: Charting Django

middleware.py

middleware.py

Page 21: Charting Django

• Irrational Exuberance @ http://lethain.com/

• django-monetize @ http://github.com/lethain/django-monetize/tree/master/

Visit my blog if you’re interested in reading about... stuff. Yeah, or don’t visit it. That’s cool too.

What isn’t cool is how smilely faces look in Marker Felt.