Posted: February 24th, 2010 | Author: Davo | Filed under: Django | Tags: getlist, querydict, request.post | 5 Comments »
The QueryDict.getlist() allows to get all the checkbox(or select list) values from the request.POST/GET object.
Let’s assume we have a simple form with the following checkboxes. Each checkbox contains an ID of an artist.
In views.py :
def handle(request):
if request.method == 'POST':
artists = request.POST.getlist('artists') # now artists is a list of [1,2,3]
Posted: February 24th, 2010 | Author: Davo | Filed under: Django | Tags: connect disconnect, Django, post_delete, post_save, pre_delete, pre_save, signals | No Comments »
Django signals allow to bind a function to a specific event.
There are 4 major signals available:
- pre_save() ( before saving )
- post_save() ( after saving )
- pre_delete() ( before deleting )
- post_delete() ( after deleting )
A simple example of connecting a signal:
# in models.py
from django.db.models.signals import post_save
from myapp.models import MyModel
def do_something(sender, **kwargs):
# the object which is saved can be accessed via kwargs 'instance' key.
obj = kwargs['instance']
print 'the object is now saved.'
# ...do something else...
# here we connect a post_save signal for MyModel
# in other terms whenever an instance of MyModel is saved
# the 'do_something' function will be called.
post_save.connect(do_something, sender=MyModel)
If you need to disconnect/detach a signal:
# instead of connect() we call disconnect()
post_save.disconnect(do_something, sender=MyModel)