django 类型错误:PaystackPayment()缺少1个位置参数:"请求"

7eumitmz  于 2022-12-24  发布在  Go
关注(0)|答案(1)|浏览(132)

我收到错误信息

PaystackPayment() missing 1 required positional argument: 'request'

在我的电子商务项目的core.views的第246行,当试图访问函数PaystackPayment()时。我不知道出了什么问题。
视图:

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

kx7yvsdv1#

视图名称(PaystackPayment)与模型类相同。

payment = PaystackPayment()

正在尝试示例化视图,而不是模型。
遵循PEP-8的建议,使用snake_case作为函数名,可以防止这个特定的错误(尽管它不是一个银,例如基于类的视图):

def paystack_payment(request):
    ...

相关问题