我得到错误
PaystackPayment() missing 1 required positional argument: 'request'
当尝试访问函数PaystackPayment()时,我的电子商务项目的core.views
的第246行。我不知道怎么了。
视图:
def PaystackPayment(request):
try:
order = Order.objects.get(user=request.user, ordered=False)
secret_key = settings.PAYSTACK_SECRET_KEY
ref = create_ref_code()
amount = int(order.get_total() * 100)
client_credentials = request.user.email
paystack_call = TransactionResource(
secret_key, ref)
response = paystack_call.initialize(
amount,
client_credentials
)
authorization_url = response['data']['authorization_url']
# create payment
payment = PaystackPayment()
payment.user = request.user
payment.ref = ref
payment.email = client_credentials
payment.amount = int(order.get_total())
payment.save()
# assign the payment to the order
order.ordered = True
order.email = client_credentials
order.ref_code = ref
order.paystack_payment = payment.amount
order.save()
# send confirmation order email to the user
subject = f'New Order Made by {request.user.username} today totaled {int(order.get_total())} '
message = 'Hello there ' +
', Thanks for your order. Your order has been received and it is being processed as we shall get back to you shortly. Thank you once again!'
email_from = settings.EMAIL_HOST_USER
recipient_list = [request.user.email, ]
send_mail(subject, message, email_from, recipient_list)
messages.success(request, 'Your order was successful')
return redirect(authorization_url)
except ObjectDoesNotExist:
messages.info(
request, """
Their was an error when you where possibly entering the checkout or payment fields.
You were not charged, try again!
""")
return redirect('core:checkout')
网址:
from django.urls import path
from .views import (PurchaseSearch, PaystackPayment, Checkout)
app_name = 'core'
urlpatterns = [
path('checkout/', Checkout.as_view(), name='checkout'),
path('make-payment/', PaystackPayment, name='payment')
模型:
class PaystackPayment(models.Model):
user = models.ForeignKey(User,
on_delete=models.SET_NULL, blank=True, null=True)
amount = models.PositiveIntegerField()
time_stamp = models.DateTimeField(auto_now_add=True)
ref = models.CharField(max_length=20)
email = models.EmailField()
payment_verified = models.BooleanField(default=False)
def __str__(self) -> str:
return f'PaystackPayment: {self.amount}'
视图名称(PaystackPayment
)与模型类相同。下面这行
payment = PaystackPayment()
试图实例化你的视图,而不是你的模型。
遵循PEP-8使用snake_case
作为函数名的建议将防止此特定错误(尽管它不是银弹,例如基于类的视图):
def paystack_payment(request):
...