def landing_page(request): if request.method == "POST": # connecting to the form # form checks if the field is empty or not and passes request.POST or None form = TaskForm(request.POST or None) # checking if the data we are provided with are valid if form.is_valid(): # save value into instance instance = form.save(commit=False) # connect to the owner of instance and set it as a user instance.owner = request.user # then save instance with a user instance.save() messages.success(request, 'Task added successfully') return redirect('landing_page') else: all_tasks = TaskList.objects.filter(owner=request.user) # dzielimy all tasks po 5 na stronę paginator = Paginator(all_tasks, 6) # w get request parameter pg pojawia się w linku page = request.GET.get('pg') all_tasks = paginator.get_page(page) return render(request, 'todolist.html', {'all_tasks': all_tasks})
def completed(request, primary_key): task = TaskList.objects.get(id=primary_key) task.completed = True task.save() if request.method == 'POST': form = TaskForm(request.POST) if form.is_valid(): form.save() return redirect('/')
def todolist(request): if request.method == 'POST': form = TaskForm(request.POST or None) if form.is_valid(): form.save() return redirect('todolist') else: all_task = Tasklist.objects.all return render(request, 'todolist.html', {'all_task': all_task})
def edit_task(request, task_id): if request.method == "POST": task = TaskList.objects.get(pk=task_id) form = TaskForm(request.POST or None, instance=task) if form.is_valid(): form.save() messages.success(request, ("Task edited!")) return redirect('todolist') else: task_obj = TaskList.objects.get(pk=task_id) return render(request, 'edit.html', {'task_obj': task_obj})
def edit_task(request, task_id): if request.method == 'POST': id = Tasklist.objects.get(pk=task_id) form_id = TaskForm(request.POST or None, instance=id) if form_id.is_valid(): form_id.save() messages.success(request, ("The task is updated")) return redirect('todolist') else: task_obj = Tasklist.objects.get(pk=task_id) return render(request, 'edit.html', {'task_obj': task_obj})
def todolist(request): if request.method == "POST": form = TaskForm(request.POST or None) if form.is_valid(): instance = form.save(commit=False) instance.owner = request.user instance.save() messages.success(request, ("New Task Added")) return redirect('todolist') else: all_tasks = TaskList.objects.filter(owner=request.user) paginator = Paginator(all_tasks, 5) page = request.GET.get('pg') all_tasks = paginator.get_page(page) return render(request, 'todolist.html', {'all_tasks': all_tasks})
def index(request): context = {} if request.method == "POST": # Get the posted form form = TaskForm(request.POST) if form.is_valid(): #form.save() #version DB r = requests.post('https://jsonplaceholder.typicode.com/todos/', params=request.POST) print(r) return redirect("index") tasks = requests.get('https://jsonplaceholder.typicode.com/todos/') print("GET*: " + str(tasks.json())) context = {'tasks': tasks.json()} return render(request, "index.html", context=context)
def edit_task(request, task_id): if request.method == 'POST': # we select a task to edit task = TaskList.objects.get(pk=task_id) # instance helps the database to recognise which task to edit form = TaskForm(request.POST or None, instance=task) if form.is_valid: form.save() messages.success(request, 'Task edited') return redirect('landing_page') else: task_obj = TaskList.objects.get(pk=task_id) return render(request, 'edit.html', {'task_obj': task_obj})
def edit_task(request, task_id): if request.method == 'POST': ## If i want add anything in db task = TaskList.objects.get(pk=task_id) form = TaskForm( request.POST or None, instance=task) ##instance=task means which task your are editing if form.is_valid(): form.save() messages.success(request, ("Task Edited Successfully")) return redirect('todolist') else: task_obj = TaskList.objects.get( pk=task_id) # it will fetch single data of that particular id return render(request, 'edit.html', {'task_obj': task_obj}) ## one obj (particular obj)
def todolist(request): if request.method == "POST": form = TaskForm(request.POST or None) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.save() messages.success(request, ("The new task is added successfully")) return redirect('todolist') else: all_task = Tasklist.objects.filter(user=request.user) paginator = Paginator(all_task, 5) page = request.GET.get('pg') all_task = paginator.get_page(page) return render(request, 'task.html', {'all_task': all_task})
def index(request): task = TaskList.objects.all() form = TaskForm() if request.method == 'POST': form = TaskForm(request.POST) if form.is_valid(): form.save() return redirect('/') context = {'form': form, 'tasks': task} return render(request, 'todolist/index.html', context)
def todolist(request): if request.method == 'POST': ## If i want add anything in db form = TaskForm(request.POST or None) ## If i want add anything in db if form.is_valid(): ## If i want add anything in db instance = form.save( commit=False) ## commit=False, to access manager instance.manager = request.user instance.save() messages.success(request, ("Task Added Successfully")) return redirect('todolist') else: # all_tasks = TaskList.objects.all() # it will fetch all the data inside all_task object(if i want to access anything) all_tasks = TaskList.objects.filter( manager=request.user) ## Task of logged in user only paginator = Paginator(all_tasks, 7) page = request.GET.get('pg') all_tasks = paginator.get_page(page) return render(request, 'todolist.html', {'all_tasks': all_tasks}) ## all obj
def create_task(request, pk): todo_list = get_object_or_404(ToDoList, pk=pk) # todo_list = todo.id if request.method == "POST": form = TaskForm(request.POST) if form.is_valid(): task = form.save(commit=False) task.todo_list = todo_list task.save() return redirect('list_view', pk=todo_list.id) else: form = TaskForm() return render(request, 'todolist/create_task.html', {'form': form})