Login

Snippets by jakecr

Snippet List

DRY template rendering decorator update

Based fully on [snippet 1929](http://www.djangosnippets.org/snippets/1929/) **The update is:** It checks to see the view returns an instance of HttpResponse and returns that rather than trying to render it. This allows you to return something like `HttpResponseRedirect('/')`, or use a normal `render_to_response` to use a different template. *Also updates cleandict as per comment on original snippet* In this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps.

  • template
  • decorator
  • rendering
  • dry
Read More

Model Hooks

Runs model methods on save, create, update, delete Similar to Rails hooks **Usage:** *in models.py* from myproject.hooks import connect_hooks class MyModel(models.Model): #... # only on first save of a newly created object def before_create(self): print self def after_create(self): print self # not on first save of a newly created object def before_update(self): print self def after_update(self): print self # any save, new object or update def before_save(self): print self def after_save(self): print self # delete, self is still available after delete def before_delete(self): print self def after_delete(self): print self **connect_hooks(MyModel)**

  • model
  • hooks
Read More

jakecr has posted 2 snippets.