Django:调用一个view函数到另一个view函数



我的待办事项列表有两个不同的功能

add_task,view_task

  • 我不想创建一个单一的函数

  • 我想让视图任务调用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,
})

为什么不直接将任务列表添加到add_task视图中呢?

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})

相关内容

  • 没有找到相关文章

最新更新