更新时间:2023-02-12 08:20:04
您可能不能,因为这取决于用户与您的表单的交互:您的服务器无法提前知道您的用户在发送表单之前将选择哪个元素到浏览器.您可能可以使用 ajax 来实现这一点.我认为一个工作流程可能是:
You probably can't because it depends of user interaction with your form: your server can't know in advance which element your user will select before sending the form to the browser. You could probably achieve this using ajax. I think a working process could be :
make
字段 隐藏
AjaxMakeFieldView
),该视图将捕获采用 vehicle_type
参数的 ajax 请求并返回 make 字段的 HTML代码>,填充相关数据.在您的 URLConf 中为此视图添加一个 URL.
vehicle_type
时,浏览器将向 AjaxMakeFieldView
发送一个 ajax 请求并替换隐藏的 make
返回 HTML 的字段make
field hiddenAjaxMakeFieldView
) that will catch an ajax request taking a vehicle_type
argument and return the HTML for make field
, populated with relevant data. Add a URL in your URLConf for this view.vehicle_type
, the browser will send aan ajax request to AjaxMakeFieldView
and replace hidden make
field with returned HTML如果您不想要 javascript,另一种方法是两步式:
If you don't want javascript, another way would be a two step form :
vehicle_type
字段的第一个表单make
字段的第二个表单,根据在第一个表单中选择的 vehicle_type
填充初始数据.vehicle_type
fieldmake
field, which initial data is populated depending of vehicle_type
selected in the first form.我从未这样做过,但是 Django 文档表单向导似乎是一个不错的起点.
I've never done this, but Django documentation on Form wizard seems a good place to start.