django - Django 表单与 HTML 表单
问题描述
当我使用 Django
{{ forms }}
我使用内置功能呈现的表单,如果用户提交并且字段无效,则清除无效字段但未清除表单中的其余信息,这使用户只能更改引发错误的字段。
当我在提交时使用 HTML 表单时,如果出现错误,所有字段都会被清除,用户必须从头开始重新编写所有信息。
如何在 html 表单中完成与 Django 中相同的功能
{{ forms }}
解决方案
您必须在视图中定义表单处理。例子:
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)
推荐阅读
- r - 如何摆脱 Rmd 中 geom_smooth 的评论?
- kdb - KDB+/Q:如何选择字典中的所有项(键+值),其中相应的值符合条件?
- c# - 在 .NET Core 中运行多个 sql 查询以获取矩阵结果
- flutter - Flutter 小部件与 Flutter 驱动程序
- arrays - 使用 VB.net 和 Newtonsoft 对值矩阵进行 JSON 反序列化
- laravel - 通过 Ajax 方法渲染 Laravel 组件
- c# - SynchronizationContext 是否不再与 ExecutionContext 一起流动(从 .NET Framework 到 .NET Core)?
- javascript - 如何使前端页面离线(只访问本地主机和相对路径资产(js,css,png...))
- unix - UNIX 中的链接
- r - 无法 install.packages('arrow') 读取镶木地板文件 (read_parquet)。任何其他方式来读取镶木地板文件或使用任何不同的库?