daily pastebin goal
37%
SHARE
TWEET

todo add/create

a guest Sep 12th, 2018 53 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. :
  2.     def get(self, request):
  3.         return render(request, "todo/todoitem_form.html", {
  4.             'form': TodoItemForm()
  5.         })
  6.  
  7.     def post(self, request):
  8.         form = TodoItemForm(request.POST)
  9.         if form.is_valid():
  10.             description = request.POST['description']
  11.             completed = 'completed' in request.POST and request.POST['completed'] == 'on'
  12.             TodoItem.objects.create(description=description, completed=completed)
  13.             # Another way: TodoItem.objects.create(description=form.cleaned_data['description'],
  14.             # completed=form.cleaned_data['completed'])
  15.             return redirect('todo-list')
  16.         return render(request, "todo/todoitem_form.html", {
  17.             'form': form
  18.         })
  19.  
  20.  
  21. def create_edit(request, pk=None):
  22.     if request.method == 'GET':
  23.         item = TodoItem() if pk is None else TodoItem.objects.get(pk=pk)
  24.         return render(request, "todo/todoitem_form.html", {
  25.             'form': TodoItemForm(instance=item)
  26.         })
  27.     if request.method == 'POST':
  28.         form = TodoItemForm(request.POST)
  29.         if form.is_valid():
  30.             return redirect('todo-list')
  31.         return render(request, "todo/todoitem_form.html", {
  32.             'form': form
  33.         })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top