'How do i use django and python request to make a post request Properly

I am using django and python request to make a post request to this endpoint with the authorization header and body gotten from this website. i want your assist to get this difficulty proper and to make this code work.

class RechargeData(models.Model, Main):
     user = models.ForeignKey(User, default=1,on_delete=models.CASCADE)
     phone_number = models.CharField(validators=[Main.phone_regex], max_length=10) 
     network = models.CharField(choices=Main.OPERATORS, max_length=15)
     plan = models.IntegerField(choices=Main.PLANS)
     amount = models.DecimalField(max_digits=10, decimal_places=2) 

i have the view.py file that contains the python request as follows

import requests import json from .forms import RechargeForm

def rechargedata(request):
  url = "http://127.0.0.1:8000/api/data/"
  payload = "{\"network\": network_id,\n\"mobile_number\": \"09037346247\",\n\"plan\": plan_id}" 
  headers = {'Authorization': 'Token 5fd60vcdfddfxddsssfddff9a0a8742d','Content-Type': 'application/json'}

  response = requests.request("POST", url, headers=headers, data=payload)

  if request.method == "POST":
      form = RechargeForm(request.POST)
      if form.is_valid():
          phone_number = form.cleaned_data['phone_number']
          network = form.cleaned_data['network']
          amount = form.cleaned_data['amount']
          plan = form.cleaned_data['plan']
      form.save

      context = RequestContext(request, {
          'phone_number': response.data.mobile_number,
          'network': response.data.network_id, 
          'mobile_number': response.data.network_id,
          'plan': response.data.plan_id,
          })
      return render(request, 'embeds.html', {'context': context, 'form'; form})
   else:
        form = RechargeForm()

   return render(request, 'index.html', { context, 'form': form})

This code is not working and am sure not to be getting something proper here.

can someone help me out?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source