def form_process_view(request):
    if request.method == 'POST':
        # 请求为 POST,利用用户提交的数据构造一个绑定了数据的表单
        form = Form(request.POST)

        if form.is_valid():
            # 表单数据合法
            # 进行其它处理...
            # 跳转
            return redirect('/')
    else:
        # 请求不是 POST,构造一个空表单
        form = Form()

    # 渲染模板
    # 如果不是 POST 请求,则渲染的是一个空的表单
    # 如果用户通过表单提交数据,但是数据验证不合法,则渲染的是一个带有错误信息的表单
    return render(request, 'template.html', context={'form': form})

 

相关文章:

  • 2022-12-23
  • 2021-05-16
  • 2021-04-17
  • 2021-10-08
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
猜你喜欢
  • 2020-04-17
  • 2021-08-04
  • 2021-08-15
  • 2018-05-03
相关资源
相似解决方案