def index(request): # See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032 class RequiredFormSet(BaseFormSet): def __init__(self, *args, **kwargs): super(RequiredFormSet, self).__init__(*args, **kwargs) for form in self.forms: form.empty_permitted = False # si se fija aca le puse extra 3 para que me pinte 3 formularios de tareas al mismo tiempo # si se lo quito pues pone solo uno prueben TodoArticuloFormSet = formset_factory(TodoArticuloForm, extra=3, max_num=10, formset=RequiredFormSet) if request.method == "POST": todo_form = TodoForm(request.POST) todo_articulo_formset = TodoArticuloFormSet(request.POST, request.FILES) if todo_form.is_valid() and todo_articulo_formset.is_valid(): todo_lista = todo_form.save() for form in todo_articulo_formset.forms: todo_item = form.save(commit=False) todo_item.fktodo = todo_lista todo_item.save() return HttpResponseRedirect("/") else: todo_form = TodoForm() todo_articulo_formset = TodoArticuloFormSet() todo = Todo.objects.all().order_by("id") # Select * from Todo ; return render_to_response("index.html", locals(), RequestContext(request))
def todo_new(request): url = reverse('todo_new') if request.method == 'GET': form = TodoForm() context = {'form': form, 'url': url} return render(request, 'todo/todo_new.html', context) elif request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): messages.add_message(request, messages.INFO, 'Successfully created todo') data = form.cleaned_data if data['due_date']: todo = Todo.objects.create( name=data['name'], is_completed=data['is_completed'], due_date=data['due_date'], notes=data['notes'], user=request.user) else: todo = Todo.objects.create( name=data['name'], is_completed=data['is_completed'], notes=data['notes'], user=request.user) context = {'todo': todo} return render(request, 'todo/todo_detail.html', context) else: context = {'form': form, 'url': url} return render(request, 'todo/todo_new.html', context)
def todo_edit(request, pk): if request.method == 'GET': todo = get_object_or_404(Todo, pk=pk) url = reverse('todo_edit', args=(todo.id,)) initial_data = {'id': todo.id, 'name': todo.name, 'notes': todo.notes, 'is_completed': todo.is_completed, 'due_date': todo.due_date} form = TodoForm(initial=initial_data) context = {'form': form, 'submit_url': url} return render(request, 'todo/todo_new.html', context) elif request.method == 'POST': form = TodoForm(request.POST) todo = get_object_or_404(Todo, pk=pk) url = reverse('todo_edit', args=(todo.id,)) if form.is_valid(): data = form.cleaned_data todo.name = data['name'] todo.is_completed = data['is_completed'] todo.notes = data['notes'] todo.due_date = data['due_date'] todo.save() messages.add_message(request, messages.INFO, 'Successfully updated todo') context = {'todo': todo} return redirect('todo_detail', todo.id) else: context = {'form': form, 'submit_url': url} return render(request, 'todo/todo_new.html', context)
def add_or_update_task(request, pk=None): task = get_object_or_404(Todo, pk=pk) if pk else None form = TodoForm(request.user, request.POST or None, instance=task) if form.is_valid(): form.save() return redirect(reverse('todo:home')) return render(request, 'todo/task_form.html', {'form': form})
def todoedit(request, pk): todo = get_object_or_404(Todo, pk=pk) todoform = TodoForm(request.POST or None, instance=todo) if todoform.is_valid(): todoform.save() return redirect('todolist') return render(request, 'todoadd.html', {'form': todoform})
def addTodo(request): form = TodoForm(request.POST) if form.is_valid(): new_todo = Todo(text=request.POST['text']) new_todo.save() return redirect('index')
def index(request): # See http://stackoverflow.com/questions/2406537/django-formsets-make-first-required/4951032#4951032 class RequiredFormSet(BaseFormSet): def __init__(self, *args, **kwargs): super(RequiredFormSet, self).__init__(*args, **kwargs) for form in self.forms: form.empty_permitted = False #si se fija aca le puse extra 3 para que me pinte 3 formularios de tareas al mismo tiempo #si se lo quito pues pone solo uno prueben TodoArticuloFormSet = formset_factory(TodoArticuloForm, extra=3, max_num=10, formset=RequiredFormSet) if request.method == 'POST': todo_form = TodoForm(request.POST) todo_articulo_formset = TodoArticuloFormSet(request.POST, request.FILES) if todo_form.is_valid() and todo_articulo_formset.is_valid(): todo_lista = todo_form.save() for form in todo_articulo_formset.forms: todo_item = form.save(commit=False) todo_item.fktodo = todo_lista todo_item.save() return HttpResponseRedirect('/') else: todo_form = TodoForm() todo_articulo_formset = TodoArticuloFormSet() todo = Todo.objects.all().order_by('id') #Select * from Todo ; return render_to_response('index.html', locals(), RequestContext(request))
def todo_create(request): if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): form.save() return redirect('todo_list') form = TodoForm() return render(request, 'todo/edit.html', {'form': form})
def test_form_is_valid(self): obj = Todo.objects.create(text='Do this', owner=self.user) data = {'text': obj.text} form = TodoForm(data=data) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data.get('text'), obj.text) self.assertNotEqual(form.cleaned_data.get('text'), 'Sleep')
def add(request): form = TodoForm(request.POST or None) if form.is_valid(): form.save() return http.HttpResponseRedirect(reverse('todo.list')) return render_to_response('add.html', {'form': form}, context_instance=RequestContext(request))
def add(request): form = TodoForm(request.POST or None) if form.is_valid(): form.save() return http.HttpResponseRedirect(reverse('todo.list')) return render_to_response('add.html', {'form':form}, context_instance=RequestContext(request))
def test_valid(self): """バリデーションOK""" params = { 'title': 'title-1', 'expiration_date': self.today, } form = TodoForm(params) self.assertTrue(form.is_valid())
def todoadd(request): todoform=TodoForm(request.POST or None) if todoform.is_valid(): todo = todoform.save() todo.user = request.user todo.save() return redirect('todolist') return render(request,'todoadd.html',{'form':todoform})
def edit(request, pk): instance = get_object_or_404(Todo, pk=pk) form = TodoForm(request.POST or None, instance=instance) if form.is_valid(): form.save() return http.HttpResponseRedirect(reverse('todo.list')) return render_to_response('edit.html', {'form': form}, context_instance=RequestContext(request))
def edit(request, pk): instance = get_object_or_404(Todo, pk=pk) form = TodoForm(request.POST or None, instance=instance) if form.is_valid(): form.save() return http.HttpResponseRedirect(reverse('todo.list')) return render_to_response('edit.html', {'form':form}, context_instance=RequestContext(request))
def create_todo(request): if request.method == 'GET': form = TodoForm(user=request.user) elif request.method == 'POST': form = TodoForm(request.POST, user=request.user) if form.is_valid(): form.save() return HttpResponseRedirect('/') return render(request, "todo_form.html", {'form': form})
def create_todo(request): form = TodoForm(request.POST) if form.is_valid(): todo = TodoList(name=form.cleaned_data['name'], description=form.cleaned_data['description'], is_done=False) todo.save() return redirect('todos index') return home(request, form)
def post(self, request, pk): task = get_object_or_404(self.model, pk=pk) form = TodoForm(request.POST, instance=blog) if not form.is_valid(): ctx = {'task': task.name, 'desc': task.desc} return render(request, self.template, ctx) form.save() return redirect(self.success_url)
def test_valid_if_expiration_date_is_not_specified(self): """バリデーションOK 期限日が指定されていない場合 """ params = { 'title': 'title-1', } form = TodoForm(params) self.assertTrue(form.is_valid())
def todo_edit(request, pk): todo = List.objects.get(user=request.user, pk=pk) if request.method == "POST": form = TodoForm(request.POST, instance=todo) if form.is_valid(): form.save() return redirect('todo_view', todo.pk) else: form = TodoForm(instance=todo) return render(request, 'todo/edit.html', {'menu': 'list', 'form': form, 'pk': pk})
def todo_create(request): if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): todo = form.save(commit=False) todo.user = request.user todo.save() return redirect('todo_view', todo.pk) else: form = TodoForm() return render(request, 'todo/edit.html', {'menu': 'list', 'form': form})
def test_invalid_if_title_is_empty(self): """バリデーションNG タイトルが空文字の場合 """ params = { 'title': '', 'expiration_date': self.today, } form = TodoForm(params) self.assertFalse(form.is_valid())
def todo_add(request): if request.method == 'POST': form = TodoForm(request.POST, request.FILES) if form.is_valid(): todo = form.save() return redirect('todo_detail', pk=todo.pk) form = TodoForm() context = { 'form': form, } return render(request, 'todo/todo_create.html', context)
def todoView(request): if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): item = form.save(commit=False) item.createUser = request.user.username item.save() items = Todo.objects.filter(createUser=request.user.username).reverse() form = TodoForm() return render(request, 'todo.html', {'items': items, 'form': form})
def todo_edit(request, pk): task = Todo.objects.get(pk=pk) if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): task.description = request.POST.get('description') task.status = request.POST.get('status') task.save() return redirect('todo_list') form = TodoForm(instance=task) return render(request, 'todo/edit.html', {'form': form})
def add(request): if request.user.is_authenticated: user = request.user form = TodoForm(request.POST) if form.is_valid(): todo = form.save(commit=False) todo.user = user todo.save() return redirect('home') else: return render(request, 'todo/home.html', context={'form': form})
def new_todo(request): if not request.method == 'POST': return redirect('home') todo_form = TodoForm(request.POST) if todo_form.is_valid(): todo_form.save() messages.add_message(request, messages.SUCCESS, 'Item has been added.') else: messages.add_message(request, messages.ERROR, 'You need to enter a name.') return redirect('home')
def update(request, id): if request.method == 'POST': todo = get_object_or_404(Todo, pk=id) todoForm = TodoForm(request.POST,instance=todo) if todoForm.is_valid(): todoForm.save() context = { 'message': 'Update article ' + str(id), 'todo': todo, } return HttpResponseRedirect('/todo/',context)
def edit_todo(req, pk): todo = TodoList.objects.get(pk=pk) if req.method == 'GET': form = TodoForm(initial=todo.__dict__) return home(req, form, 'edit todo', pk=pk) else: form = TodoForm(req.POST) if form.is_valid(): todo.name = form.cleaned_data['name'] todo.description = form.cleaned_data['description'] todo.save() return home(req, form)
def AddTodo(request): if not request.user.is_authenticated(): return redirect('accounts:login') if request.method == "POST": form = TodoForm(request.POST) if form.is_valid(): todo = form.save(commit=False) todo.user = request.user todo.save() return IndexView(request) else: form = TodoForm() return render(request, 'addtodo.html', {'form': form})
def add_todo(request): """Add a new todo. Should be invoked via POST method.""" if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): # should log the action form.save() messages.success(request, f"Added \"{request.POST['description']}\"") return redirect('todo:index') else: # GET /add/ should be handled by the main index return redirect('todo:index') return render(request, 'todo/add.html', {'form': form})
def add_todo(request): form = TodoForm() if request.method == 'POST': print("Post") form = TodoForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): print(form.is_valid()) # Save the new item to the database. form.save(commit=True) print("balh") # Now that the category is saved # We could give a confirmation message # But since the most recent category added is on the index page # Then we can direct the user back to the index page. return index(request) else: # The supplied form contained errors - # just print them to the terminal. print(form.errors) # Will handle the bad form, new form, or no form supplied cases. # Render the form with error messages (if any). return render(request, 'todo/add_todo.html', {'form': form})
def addItem(request): t = Todo() t.text = request.POST.get('todo_text', '') t.save() if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/') #Item.objects.create(text=request.POST['item_text']) #return redirect('/') else: return render(request, 'todo/create.html', {'form': form})
def create_todo(request): args = {} args.update(csrf(request)) args['user'] = request.user if request.method == 'POST': form = TodoForm(request.POST) if form.is_valid(): todo = form.save(commit=False) todo.user = request.user todo.save() return HttpResponseRedirect('/todo/all') else: form = TodoForm() args['form'] = form return render_to_response('create_todo.html', args)
def edit_todo(request: HttpRequest, todo_id: int = 0): context = {'todo_lists': TodoList.objects.filter(user=request.user).all()} todo = get_object_or_404(Todo, pk=todo_id) if todo.todo_list.user != request.user: raise Http404 if request.method == 'POST': form = TodoForm(request.POST, instance=todo) if not form.is_valid(): context['form'] = form return render(request, 'edit_todo.html', context) todo = form.save() return redirect('view_list', todo.todo_list.id) form = TodoForm(instance=todo) context['form'] = form return render(request, 'edit_todo.html', context)
def create_todo(request: HttpRequest, list_id: int = 0): context = {'todo_lists': TodoList.objects.filter(user=request.user).all()} todo_list = get_object_or_404(TodoList, pk=list_id) if request.method == 'POST': form = TodoForm(request.POST) if not form.is_valid(): context['form'] = form return render(request, 'create_todo.html', context) todo = form.save(commit=False) todo.todo_list = todo_list todo.save() return redirect('view_list', todo_list.id) form = TodoForm() context['form'] = form return render(request, 'create_todo.html', context)
def ManageAddView(request, username=''): user = User.objects.get(username=username) form = TodoForm(request.POST or None) if form.is_valid(): thisTodo = form.cleaned_data['todo'] thisPriority = form.cleaned_data['priority'] thisUser = User.objects.get(username=username) todo = Todo(user=thisUser, todo=thisTodo, priority=thisPriority, status=1) todo.save() return HttpResponseRedirect(URL_TODO_MANAGE(user.username)) return HttpResponseRedirect(URL_TODO_MANAGE(user.username))
def post(self, request, pk): form = TodoForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] status = form.cleaned_data['status'] todo = self.query_set(pk=pk) todo.title = title todo.content = content todo.status = status todo.save() return HttpResponseRedirect( reverse(viewname='todo_detail', kwargs={'pk': pk})) return HttpResponseRedirect(reverse(viewname='edit', kwargs={'pk': pk}))
def edit_task(request, task_id): if request.method == "POST": task = TodoList.objects.get(pk=task_id) form = TodoForm(request.POST or None, instance=task) if form.is_valid(): form.save() messages.success(request, ("Task Edited!")) return redirect('addTodo') else: form = TodoForm() # for checking status of task task_obj = TodoList.objects.get(pk=task_id) return render(request, 'edit.html', { 'task_obj': task_obj, 'form': form })
def ManageUpdateView(request, username='', todoid=''): todo = Todo.objects.get(id=todoid) user = User.objects.get(username=username) form = TodoForm(request.POST or None) if form.is_valid(): todo.todo = form.cleaned_data['todo'] todo.priority = form.cleaned_data['priority'] todo.save() return HttpResponseRedirect(URL_TODO_MANAGE(user.username)) else: template_name = 'todo/manager_update.html' template_value = {'form': form, 'todo': todo, 'user': user} return render_to_response(template_name, template_value, context_instance=RequestContext(request))
def test_form_invalid(self): todo = TodoForm() self.assertEqual(todo.is_valid(),False)
def test_form(self): todo = TodoForm({"activity":"esto es un texto"}) self.assertEqual(todo.is_valid(), True)
def _post(self, request): form = TodoForm(request.POST, instance=Todo()) if form.is_valid(): # get data from form # form.cleaned_data.get('title') form.save()
def _update(self, data): todo = Todo.get_todos(data.get('id')) data.update({'completed': json.loads(data.get('completed'))}) form = TodoForm(data, instance=todo) if form.is_valid(): form.save()