【问题标题】:Django forms vs HTML formsDjango 表单与 HTML 表单
【发布时间】:2021-04-01 08:04:33
【问题描述】:

当我使用 Django 时

{{ forms }}

我使用内置功能呈现的表单,如果用户提交并且字段无效,则清除无效字段但未清除表单中的其余信息,这使用户只能更改引发错误的字段.

当我在提交时使用 HTML 表单时,如果出现错误,所有字段都会被清除,用户必须从头开始重新编写所有信息。

如何在 html 表单中实现与 Django 中相同的功能

{{ forms }}

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    您必须在视图中定义表单处理。示例:

    class UserLoginView(View):
        def get(self, request):
            # here we process GET request
    
        def post(self, request):
            form = AuthenticationForm(request, data=request.POST)
            if form.is_valid():
                # do something
            else:
                # save form with data as context, and render it 
                context = {'form': form}
                return render(request, 'customers/login.html', context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2017-02-17
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2012-08-23
      相关资源
      最近更新 更多