Django Patterns slides (7 of 86)

Some patterns can be turned in to constructs

generic views

render_to_response

new redirect introduced in Django 1.1

view decorators

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Django Patterns slides (5-6 of 86)

Example

Change Log Model

not a reusable model, more parametrized snippet

class LogMessage(models.Model):
    thing = models.ForeignKey(Thing)
    user = models.ForeignKey(User) 
    timestamp = models.DateTimeField(default=datetime.now) 
    message = models.TextField()

This is simple LogMessage model. On every change LogMessage is added with the info about which thing is changed, who did the action, current date and time (note missing parenthesis after now, default gets function, not value) and info about the change. It's not reusable (you cannot use this snippet to watch on changes on other models) and also if some thing can be deleted, you need to think how to log that. You can use it with signals.

 

 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Django Patterns slides (4 of 86)

Example

environment-specific settings

try:
    from local_settings import *
except ImportError:
    pass

You can use this in settings.py to set your specific settings. The idea is to keep local_settings.py files out of repository (put it in .gitignore, for example).

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Hello world!

print "Hello world!"
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS