【发布时间】:2014-05-07 19:48:32
【问题描述】:
我有一个 FormView 如下:
class SignUpView(FormView):
template_name = '/accounts/signup.html'
form_class = SignUpForm
def get_context_data(self, **kwargs):
context = super(SignUpView, self).get_context_data(**kwargs)
# code..
return context
def get_initial(self):
initial = super( SignUpView, self ).get_initial()
# code..
initial = {
'email': email,
'dob_mm': dob_mm,
'dob_dd': dob_mm,
'dob_yyyy': dob_yyyy,
'email_conf': "",
'password': "",
}
return intial
def form_valid(self, form):
# hit external APIs to validate, charge card, etc.
# if any errors from APIs, return to SignUp form
messages.error( self.request, errors )
return redirect('/account/signup/')
此时,如果页面被重新加载(由于任何错误),表单将重新初始化为空白,除了我在 get_initial() 中准备的一些字段。有什么方法可以自动保留输入字段,还是我必须手动存储它们,检测到由于错误而刷新,然后在 get_initial() 中手动设置所有字段?
我找不到任何可以描述如何使用 FormView 进行操作的帖子或文档。谢谢!
【问题讨论】:
-
为什么不实施客户端验证来过滤掉大部分无效字段条目?如果存在无效条目,您可以禁用提交按钮,然后依靠服务器端验证进行最终处理。
-
我这样做。我所指的错误可能来自外部 API——例如用户已经在场,付款不成功,等等——所以我无法从客户端检查这些..