【问题标题】:Python how to pass form input value to payloadPython如何将表单输入值传递给有效负载
【发布时间】:2022-07-08 12:51:39
【问题描述】:

我需要向 API 提交表单,并且我想将表单字段值传递给 Python 请求 API 的 Payload,我该如何实现?

models.py

class Book(models.Model):
trading_name = models.CharField(max_length=100, null=True)
industry = models.CharField(max_length=100, null=True)
vat = models.CharField(max_length=100, null=True)
address_street = models.CharField(max_length=100, null=True)
address_city = models.CharField(max_length=100, null=True)
address_postal_code = models.CharField(max_length=100, null=True)
address_iso_country = CountryField(max_length=100, null=True)


def __str__(self):
    return self.title

forms.py

class BookForm(forms.ModelForm):
class Meta:
    model = Book
    fields = ('trading_name', 'industry', 'vat', 'address_street',
              'address_city', 'address_postal_code', 'address_iso_country')

views.py

def upload_kyb(request):
if request.method == 'POST':
    url = 'https://stoplight.io/mocks/railsbank/api/25652867/v1/customer/endusers'
    headers = {
              'Content-Type': "application/json",
              'Accept': "application/json",
              'Authorization': ""
        }
    payload = "{ \n \"company\": {\n    \"name\": \"Example Company\", \n    \"trading_name\": \"jiuliasu\",\n    \"industry\": \"Telecommunications\",\n    \"vat\": \"GB123456789\",\n    \"registration_address\": \n      {\n         \"address_refinement\": \"Floor 15\", \n         \"address_number\": \"20\", \n         \"address_street\": \"Floor 15\", \n         \"address_city\": \"London\", \n         \"address_postal_code\": \"SS8 9JH\", \n         \"address_iso_country\": \"GBR\" \n      }\n\t}\n}"
    response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
    print(response.text)
    print(payload)
    # form = Kybform(request.POST, request.FILES)
    form = BookForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        return redirect('kyb_list')
else:
    
    form = BookForm()
return render(request, 'kybstart/upload_kyb.html', {
    'form': form
})

我可以在控制台中获得响应,但我如何才能真正将表单输入动态传递到有效负载中?

【问题讨论】:

    标签: python django payload


    【解决方案1】:

    为帖子中的项目分配变量,然后使用有效负载中的变量

    你可以用引号分解字符串并连接变量。

    示例 Payload = "交易名称:" + name_variable +"行业"

    【讨论】:

      【解决方案2】:

      使用 f-string 对有效负载进行排序:

      payload = f'{{  "company": {nl}, "name": {trading_name}, {nl}"industry": {industry}, {nl}"vat": {vat}, {nl}, "registration_address": {nl}, "address_number": {address_street}, {nl}"address_street": {address_city}}}'
      

      并根据上面的答案分配变量

      trading_name = request.POST.get('trading_name')
          nl = '\n'
          industry = request.POST.get('industry')
          vat = request.POST.get('vat')
          address_street = request.POST.get('address_street')
          address_city = request.POST.get('address_city')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多