I am trying to integrate a payment system in my Django project and I models for profile, and submitted. The issue is that I want a situation where when a user clicks on pay button the system should check whether he/she has submitted application and if so; grab the username, email, phone, amount and pass them as arguments into my process_payment view where the payment is going to be done.
here is code for Profile model:
class Profile(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
surname = models.CharField(max_length=10, null=True)
othernames = models.CharField(max_length=30, null=True)
gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)
nation = models.CharField(max_length=255, choices=NATION, blank=True, null=True)
state = models.CharField(max_length=20, null=True)
address = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=16, null=True)
image = models.ImageField(default='avatar.jpg', upload_to ='profile_images')
here is code for Scholarship model:
class Submitted(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
application = models.UUIDField(primary_key = True, editable = False, default=uuid.uuid4)
confirm = models.BooleanField()
approved = models.CharField(max_length=20, null=True)
date = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
self.application == str(uuid.uuid4())
super().save(*args, **kwargs)
def __unicode__(self):
return self.applicant
def __str__(self):
return f'Application Number: {self.application}-{self.applicant}'
Here is my view code:
@login_required(login_url='user-login')
def scholarship_detail(request, pk):
data = Scholarship.objects.get(id=pk)
if request.method=='POST':
applicant= request.user
email = '[email protected]'
amount = 2
phone = 8034567
return redirect(str(process_payment(applicant,email,amount,phone)))
else:
form = PaymentForm()
ctx={
'scholarship':data
}
return render(request, 'user/scholarship.html', ctx)
How can this logic be implemented efficiently because my trial is saying process_payment() missing 3 required positional arguments: 'email', 'amount', and 'phone'. Thanks for your answer
CodePudding user response:
You should add the process_payment
parameters to a dictionary(context) and return process_payment
. then all other operations will happen inside process_payment
@login_required(login_url='user-login')
def scholarship_detail(request, pk):
data = Scholarship.objects.get(id=pk)
if request.method=='POST':
applicant= request.user
email = '[email protected]'
amount = 2
phone = 8034567
context = {'applicant':applicant, 'email':email, 'amount':amount, phone} # Add to context. this will grab all the details into `process_payment` view
return process_payment(request, context)
else:
form = PaymentForm()
ctx={
'scholarship':data
}
return render(request, 'user/scholarship.html', ctx)
process_payment
view or function will look similar to:
def process_payment(request, newContext={}):
print(newContext)
# Process the payment here
return render(request, 'payment.html', newContext)
This should do the trick, but let me know if you have more questions