简体   繁体   English

Python如何将表单输入值传递给有效负载

[英]Python how to pass form input value to payload

I need to submit a form to an API and I want to pass the form fields value to the Payload for a python request API, how can I achieve this?我需要向 API 提交表单,并且我想将表单字段值传递给 Python 请求 API 的 Payload,我该如何实现?

models.py模型.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表格.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视图.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
})

I can get the response in the console but how can I actually pass the form inputs dynamically into the Payload?我可以在控制台中获得响应,但我如何才能将表单输入动态传递到有效负载中?

Assign variables to the items in your post then use the variables in your payload为帖子中的项目分配变量,然后使用有效负载中的变量

You can break up the string with quotes and concatenate the variables.您可以用引号分解字符串并连接变量。

Example Payload = "Trading name: " + name_variable +" industry"示例 Payload = "交易名称:" + name_variable +"行业"

sorted using f-string for the payload:使用 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}}}'

and assign variable as per the answer above并根据上面的答案分配变量

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM