代码之家  ›  专栏  ›  技术社区  ›  user987

如何在Django中访问提交的表单请求数据

  •  1
  • user987  · 技术社区  · 5 月前

    我想要提交数据的价值 init .我可以通过cleaned_data获取form.valid()后的数据,但无法访问中的数据 init 提交表格后

    form.py

    class MyForm(forms.Form):
    
        def __init__(self, *args, **kwargs):
            // Want to access submitted source and medium data here
            super(MyForm, self).__init__(*args, **kwargs)
            // or Want to access submitted source and medium data here
    

    view.py

    我得到了两个不同的值 source medium 在里面 request.GET

    myform = MyForm(request.GET)
    
    1 回复  |  直到 5 月前
        1
  •  1
  •   willeM_ Van Onsem    5 月前

    数据只是第一个参数,因此我们可以使用:

    class MyForm(forms.Form):
    
        def __init__(self, data=None, *args, **kwargs):
            if data is not None and 'source' in data:
                print(data['source'])
            super().__init__(data, *args, **kwargs)