Django视图Formview.get()执行两次__init__代码,这是正确的方法吗?

时间:2019-11-10 05:23:21

标签: django django-forms

当django提供视图时,将使用表单类UserRegisterForm(forms.Form),并且此表单的 init 函数将执行两次。

我无法确定为什么两次执行该表单。

RegisterView覆盖FormView提供的get()函数。 如果我注释掉get函数,则正确返回Form,并且UserRegisterForm的 init 函数仅执行一次。

diff_x_y = start_points - dest_points
print(diff_x_y.shape) # (2, 4, 2)
abs_diff_x_y = np.abs(start_points - dest_points)
man_distance = np.sum(abs_diff_x_y, axis=2)
print('man_distance:\n', man_distance)
sum_distance = np.sum(man_distance, axis=0)
print('sum_distance:\n', sum_distance)

RegesterView呈现UserRegisterForm

start_points = np.array([[1,2], [2,3], [4,5], [5,6]])
dest_points = np.array([[11,13], [12, 14]])
np.sum(np.abs(start_points[np.newaxis, :, :] - dest_points[:, np.newaxis, :]), axis=(0,2))

我希望表单能够正确加载,而无需执行UserRegisterForm。 init 会执行两次。

1 个答案:

答案 0 :(得分:0)

覆盖get_context_data而不是解决我的问题。 此处找到的描述和示例是我基于以下代码的: When to use get, get_queryset, get_context_data in Django?

相关问题