使用html和视图中的数据自动填充表单

时间:2019-02-28 06:31:21

标签: django django-forms django-views

我正在尝试根据系统使用以下代码创建的预测在另一页上填写订单表格:

form.py:

class SupplierPOForm(ModelForm):

class Meta:
    model = SupplierPO
    fields = ('delivery_date', 'supplier')
    widgets = {
        'delivery_date': DateInput()
    }

    supplier = forms.CharField(max_length=200, label = 'supplier', widget = forms.Select(attrs={'id':'supplier'}))

class SupplierPOItemsForm(ModelForm):
    class Meta:
        model = SupplierPOItems
        fields = ('item', 'quantity')

        inventory = forms.ModelChoiceField(queryset=Inventory.objects.all())

        def __init__(self, *args, **kwargs):
            super(SupplierPOItemsForm, self).__init__(*args, **kwargs)
            self.fields['item'].queryset = Inventory.objects.all()

views.py:

def inventory_forecast_details(request, id):
item = Inventory.objects.get(id=id)

...

context = {
    'item': item,
    'i': i,
    'forecast_1': forecast_1,
    'forecast_2': forecast_2,
    'forecast_3': forecast_3,
    'forecast_4': forecast_4,
}
return render(request, 'inventory/inventory_forecast_details.html', context)

理想情况下,发生的情况是,当用户按下“订单”按钮时,他们将被重定向到PO表单页面,其中带有来自预测的数据的信息将自动输入到该表单页面的字段中。预测会产生日期和所需数量。

最好的方法是什么?

0 个答案:

没有答案