Django: Call a view function into another view function

my todo list has two different functions

add_task & view_task

  • I don't want to create one single function

  • I want that view task will call add task

# Add Task Page
def add_task(request):
    submitted = False
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/add_task?submitted=True')
    else:
        form = TodoForm
        if 'submitted' in request.GET:
            submitted = True
        return render(request, "core/add_task.html", {'form':form, 'submitted': submitted})

# Task List Page
def view_task(request):
    task_list = Todo.objects.all()
#This is the part of view_task function where I have been trying to call add_task function
# If you see a few lines below, I am calling already form,
    return render(request, "core/view_task.html", {
        'task_list': task_list, 
        #'form':form,
        })

Why don't you just add your list of tasks to your add_taskview?

def add_task(request):
    task_list = Todo.objects.all()
    submitted = False
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/add_task?submitted=True')
    else:
        form = TodoForm()
        if 'submitted' in request.GET:
            submitted = True
        return render(request, "core/add_task.html", {'form':form, 'task_list': task_list, 'submitted': submitted})
Back to Top