如何从不同的模型中获得登录的用户详细信息作为参数



我正试图在我的Django项目和我模型的配置文件中集成一个支付系统,并提交。问题是,当用户点击付费按钮时,系统应该检查他/她是否提交了应用程序,如果是的话;获取用户名、电子邮件、电话、金额,并将它们作为参数传递给process_payment视图,在这里支付将被完成。

是Profile模型的代码:

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

下面是奖学金模型的代码:

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

下面是我的视图代码:

@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 = 'henry@gmail.com'
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)

如何有效地实现这个逻辑,因为我的试验说process_payment()缺少3个必需的位置参数:'email', 'amount'和'phone'。谢谢你的回答

您应该将process_payment参数添加到字典(上下文)中并返回process_payment。那么所有其他操作都将在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 = 'henry@gmail.com'
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视图或功能将类似于:

def process_payment(request, newContext={}):
print(newContext)
# Process the payment here 
return render(request, 'payment.html', newContext)

这应该可以,但如果你有更多的问题,请告诉我

最新更新