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 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 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 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 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 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 view_todo(request, todo_id): todo = get_object_or_404(Todo, pk=todo_id, user=request.user) context = {"todo": todo} if request.method == "POST": try: form = TodoForm(request.POST, instance=todo) form.save() return redirect('currenttodos') except Exception: context["error"] = "Something bad happen. Please try again" context["form"] = TodoForm(instance=todo) return render(request, "todo/view-todo.html", context=context)
def viewtodo(request, todo_pk): todo = get_object_or_404(Todo, pk=todo_pk, user=request.user) if request.method == 'GET': form = TodoForm(instance=todo) return render(request,'todo/viewtodo.html',{'todo':todo, 'form':form}) else: try: form = TodoForm(request.POST, instance=todo) form.save() return redirect('currenttodos') except ValueError: return render(request,'todo/viewtodo.html',{'todo':todo, 'form':form, 'error':'Too many data input. Please try again.'})
def viewtodo(request, todo_pk): todo = get_object_or_404(Todo, pk=todo_pk, user=request.user) if request.method == 'GET': form = TodoForm(instance=todo) return render(request, 'todo/viewtodo.html', { 'todo': todo, 'form': form }) else: form = TodoForm(request.POST, instance=todo) form.save() return redirect('currenttodos')
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 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 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 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 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 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 edit_todo(request, todo_id): response = {} status = 400 if request.method == 'POST': instance = TodoItem.objects.get(pk=todo_id) form = TodoForm(request.POST, user=request.user, instance=instance) if form.is_valid(): form.save() status = 200 else: print(form.errors.items()) response['errors'] = dict(form.errors.items()) if request.method == 'DELETE': model = TodoItem.objects.get(pk=todo_id) model.delete() status = 200 return json_response(response, status)
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 createtodo(request): if request.method == "GET": return render(request, 'todo/createtodo.html', {'form': TodoForm()}) else: form = TodoForm(request.POST) newtodo = form.save(commit=False) newtodo.user = request.user newtodo.save() return redirect('currenttodos')
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 tododetail(request, todo_pk): todo = get_object_or_404(Todo, pk=todo_pk, user=request.user) if request.method == 'GET': form = TodoForm(instance=todo) return render(request, 'todo/tododetail.html', { 'todo': todo, 'form': form }) else: try: form = TodoForm(request.POST, instance=todo) form.save() return redirect('usertodos') except ValueError: error = 'Bad data passed in.' return render(request, 'todo/tododetail.html', { 'todo': todo(), 'form': form, 'error': error })
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_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 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 createtodo(request): if request.method == 'GET': return render(request,'todo/createtodo.html',{'form':TodoForm()}) else: try: form = TodoForm(request.POST) newtodo = form.save(commit=False) newtodo.user = request.user newtodo.save() return redirect('currenttodos') except ValueError: return render(request,'todo/createtodo.html',{'form':TodoForm(), 'error':'Too many data input. Please try again.'})
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 viewtodo(request, todo_pk): if request.user.is_superuser: todo_object = get_object_or_404(Todo, pk=todo_pk) else: todo_object = get_object_or_404(Todo, pk=todo_pk, user=request.user) if request.method == 'GET': form = TodoForm(instance=todo_object) return render(request, 'todo/viewtodo.html', { 'todo': todo_object, 'form': form }) elif request.method == 'POST': try: form = TodoForm(request.POST, instance=todo_object) form.save() return redirect('currenttodos') except ValueError: return render(request, 'todo/viewtodo.html', { 'todo': todo_object, 'form': form, 'error': 'Bad info.' })
def addTodo(request): if request.method == "POST": form = TodoForm(request.POST, request.FILES) if form.is_valid(): form.save() messages.success(request, ('New Task Added!')) return redirect('addTodo') else: form = TodoForm() all_tasks = TodoList.objects.all() # define paginator on object all_tasks paginator = Paginator(all_tasks, 5) page = request.GET.get('pg') # Gets page with page no in page and retrieves all contents in that page. all_tasks = paginator.get_page(page) return render(request, 'todo.html', { 'all_tasks': all_tasks, 'form': form, 'now1': datetime.now() })
def create_todo(request): context = {} if request.method == "POST": try: todo = TodoForm(request.POST) new_todo = todo.save(commit=False) new_todo.user = request.user new_todo.save() return redirect('currenttodos') except Exception: context["error"] = "Something bad happen. Please try again" context["form"] = TodoForm() return render(request, "todo/create-todo.html", context=context)
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 view_todo(request, todo_pk): """ Отображение конкретного """ context = {} todo = get_object_or_404(Todo, pk=todo_pk, user=request.user) context['todo'] = todo if request.method == 'GET': context['form'] = TodoForm(instance=todo) return render(request, 'todo/view_todo.html', context) else: try: form = TodoForm(request.POST, instance=todo) form.save() return redirect('todo:current-todos') except ValueError: context['form'] = form context[ 'error'] = 'Что-то пошло не так... Попробуйте заполнить поля заново' return render(request, 'todo/viewtodo.html', context)
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 _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()
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()