Django PayPal&其他选项

时间:2010-09-09 20:09:52

标签: django django-paypal

我正在使用Django PayPal。 PayPal有list of options你可以传递你的按钮。我正在尝试将其中一些添加到我的paypal_dict

paypal_dict = {
    # ...

    # preopulate paypal checkout page
    "email": invoice.user.email,
    "first_name": invoice.user.first_name,
    "last_name": invoice.user.last_name,
    "address1": invoice.user.address.street,
    "city": invoice.user.address.city,
    "country": invoice.user.address.get_country_display,
    "address_country_code": invoice.user.address.country
}

form = PayPalPaymentsForm(initial=paypal_dict)

但是当我检查表单时,这些字段永远不会被添加。如何才能添加它们?

3 个答案:

答案 0 :(得分:4)

答案 1 :(得分:1)

我入侵了它:

def __init__(self, button_type="buy", extra_options={}, *args, **kwargs):
    super(PayPalPaymentsForm, self).__init__(*args, **kwargs)
    self.extra_options = extra_options
    self.button_type = button_type

def render(self):
    extra_fields = u''.join(['<input type="hidden" name="%s" value="%s" />' % (escape(name), escape(value)) for name, value in self.extra_options.iteritems()])          
    return mark_safe(u"""<form action="%s" method="post">
%s
%s
<input type="image" src="%s" border="0" name="submit" alt="Buy it Now" />
</form>""" % (POSTBACK_ENDPOINT, self.as_p(), extra_fields, self.get_image()))


def sandbox(self):
    extra_fields = u''.join(['<input type="hidden" name="%s" value="%s" />' % (escape(name), escape(value)) for name, value in self.extra_options.iteritems()])  
    return mark_safe(u"""<form action="%s" method="post">
%s
%s
<input type="image" src="%s" border="0" name="submit" alt="Buy it Now" />
</form>""" % (SANDBOX_POSTBACK_ENDPOINT, self.as_p(), extra_fields, self.get_image()))

答案 2 :(得分:1)

由于Python多重继承,我找到了更清晰的解决方案而无需修改原始代码。

forms.py

from django import forms
from paypal.standard.widgets import ValueHiddenInput
from paypal.standard.forms import PayPalEncryptedPaymentsForm


class PayPalAddressFormMixin(forms.Form):

    address1 = forms.CharField(widget=ValueHiddenInput())
    address2 = forms.CharField(widget=ValueHiddenInput())
    city = forms.CharField(widget=ValueHiddenInput())
    country = forms.CharField(widget=ValueHiddenInput())
    zip = forms.CharField(widget=ValueHiddenInput())

    email = forms.CharField(widget=ValueHiddenInput())
    first_name = forms.CharField(widget=ValueHiddenInput())
    last_name = forms.CharField(widget=ValueHiddenInput())


class PayPalEncryptedPaymentsAddressForm(PayPalEncryptedPaymentsForm, PayPalAddressFormMixin):
    pass
在views.py中

,您可以照常设置初始值

paypal_dict = {
    # ...

    # preopulate paypal checkout page
    "email": invoice.user.email,
    "first_name": invoice.user.first_name,
    "last_name": invoice.user.last_name,
    "address1": invoice.user.address.street,
    "city": invoice.user.address.city,
    "country": invoice.user.address.get_country_display,
    "address_country_code": invoice.user.address.country
}

form = PayPalEncryptedPaymentsAddressForm(initial=paypal_dict)