Created
July 4, 2012 09:24
-
-
Save kuozo/3046326 to your computer and use it in GitHub Desktop.
django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
actions.get(action)(selected)代码写的不错,干净。 | |
源文件:https://github.com/marconi/django-quickstart/blob/master/src/todo/views.py | |
''' | |
def home(request): | |
todos=Todo.objects.order_by('-created') | |
if request.method == 'POST': | |
action = request.POST['action'].lower() | |
todo_list_form = TodoListForm(data=request.POST, todos=todos) | |
if todo_list_form.is_valid(): | |
selected = Todo.objects.filter(id__in=todo_list_form.cleaned_data) | |
actions = {'done': lambda items: items.update(is_done=True), | |
'delete': lambda items: items.delete()} | |
actions.get(action)(selected) | |
messages.add_message(request, messages.SUCCESS, | |
'Items has been updated.') | |
else: | |
messages.add_message(request, messages.ERROR, | |
''.join(todo_list_form.non_field_errors())) | |
return redirect('home') | |
else: | |
todo_form = TodoForm() | |
todo_list_form = TodoListForm(todos=todos) | |
context = {'todo_list_form': todo_list_form, | |
'todo_form': todo_form} | |
return render_to_response('todo/home.html', | |
context, | |
RequestContext(request)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment