你可以通过传入你自己的函数来更改/重定向 django 表单的函数吗?

发布于 2024-11-25 07:06:40 字数 982 浏览 2 评论 0原文

我正在处理 django-paypal 并想要更改按钮 src 图像。所以我进入源中的conf.py 文件并编辑了src 目标。但是,我真的想不去管源,并且我注意到它

class PayPalPaymentsForm(forms.Form):

可以

def get_image(self):
    return {
        (True, self.SUBSCRIBE): SUBSCRIPTION_SANDBOX_IMAGE,
        (True, self.BUY): SANDBOX_IMAGE,
        (True, self.DONATE): DONATION_SANDBOX_IMAGE,
        (False, self.SUBSCRIBE): SUBSCRIPTION_IMAGE,
        (False, self.BUY): IMAGE,
        (False, self.DONATE): DONATION_IMAGE,
    }[TEST, self.button_type]

处理所有图像 src 目的地。由于在源代码中更改此定义比更改conf更糟糕,我想知道是否有一种方法可以传递您所做的自定义定义,例如在表单中传递初始参数?这样就不会更改源代码,并且我可以根据需要自定义 get_image def。

传递 def 这样的东西?

def get_image(self):
    ....
    ....
paypal = {
    'amount': 10,
    'item_name': 'test1',
    'item_number': 'test1_slug',

    # PayPal wants a unique invoice ID
    'invoice': str(uuid.uuid4()), 
}
form = PayPalPaymentsForm(initial=paypal, get_image)

谢谢!

I'm dealing with django-paypal and want to change the button src images. So I went the the conf.py file in the source and edited the src destination. However, I really want to leave the source alone, and I noticed that the

class PayPalPaymentsForm(forms.Form):

has

def get_image(self):
    return {
        (True, self.SUBSCRIBE): SUBSCRIPTION_SANDBOX_IMAGE,
        (True, self.BUY): SANDBOX_IMAGE,
        (True, self.DONATE): DONATION_SANDBOX_IMAGE,
        (False, self.SUBSCRIBE): SUBSCRIPTION_IMAGE,
        (False, self.BUY): IMAGE,
        (False, self.DONATE): DONATION_IMAGE,
    }[TEST, self.button_type]

which handles all the image src destinations. Since changing this def in the source is worse than changing conf, I was wondering if there was a way to pass in customized defs you make like passing in initial arguments in forms? This way no source code is changed, and I can customize the get_image def as much as I need.

passing in def something like this?

def get_image(self):
    ....
    ....
paypal = {
    'amount': 10,
    'item_name': 'test1',
    'item_number': 'test1_slug',

    # PayPal wants a unique invoice ID
    'invoice': str(uuid.uuid4()), 
}
form = PayPalPaymentsForm(initial=paypal, get_image)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2024-12-02 07:06:40

如下所示: https://github.com/dcramer /django-paypal/blob/master/paypal/standard/conf.py

您必须在 settings.py 中更改 PAYPAL_IMAGE 或 PAYPAL_SUBSCRIPTION_IMAGE...。

As seen here: https://github.com/dcramer/django-paypal/blob/master/paypal/standard/conf.py

You have to change PAYPAL_IMAGE, or PAYPAL_SUBSCRIPTION_IMAGE, ... in your settings.py.

柒夜笙歌凉 2024-12-02 07:06:40

只需子类 PayPalPaymentsForm 并覆盖 get_image 即可。

Just subclass PayPalPaymentsForm and override get_image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文