django - Django UpdateView 中的 {{ form.object }}
问题描述
根据文档, UpdateViewself.object
存在
模板:
{% block content %}
<p>
{{ form.object.some_foreign_key }}
</p>
<form method="post">
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock %}
但form.object
不存在:
失败:“/foo/bar_form.html”中未定义的模板变量“form.object”
为什么form.object
模板中不存在?
解决方案
似乎您将UpdateView
类属性object
与注入的模板上下文form
实例混淆了
表单实例的设置方式与您执行基于函数的视图时的设置方式相同,并且self.object
设置为form.instance
从ModelFormMixin
def get_form_kwargs(self):
"""Return the keyword arguments for instantiating the form."""
kwargs = super().get_form_kwargs()
if hasattr(self, 'object'):
kwargs.update({'instance': self.object})
return kwargs
此外,您可以检查例如 get() 方法ProcessFormView
并通过实现get_context_data
inFormMixin
推荐阅读
- vue.js - Uncaught ReferenceError: process is not defined vuejs, vuex, Webpack SSR
- jenkins - 从 Jenkins Pipeline 触发 BitBucket 合并
- mysql - 如何从终端恢复mysql数据库备份?
- c# - c# - 如何在已被覆盖的自定义控件上创建事件和更改属性?
- c# - 数据库端事务拆分与应用程序端事务拆分
- bash - 如何修复此命令菜单
- c# - 如何:TcpClient NetworkStream 生命周期
- javascript - 双击“input[]:focus”上的必要性
- sql - 用以前的 NOT NULL 行更新 NULL 行
- python - ModuleNotFoundError:没有名为“app.wsgi”的模块