FormView不执行form_valid()方法
FormView does not execute the form_valid() method
我正在使用 Braintree 作为支付解决方案(沙盒)。当我使用填写表单时,它会将我重定向到同一页面。我的错误是表格没有得到处理。在 FormView 上只有 get_context_data()
被执行但没有 form_valid()
html
<script src="https://js.braintreegateway.com/web/dropin/1.6.1/js/dropin.min.js"></script>
<form action="{% url 'checkout_braintree' %}" method="POST" id="payment-form">
{% csrf_token %}
<h3>Method of Payment</h3>
<p>378282246310005</p>
<input type="hidden" id="nonce" name="payment_method_nonce" />
<div class="bt-drop-in-wrapper">
<div id="bt-dropin"></div>
</div>
<button class="button" type="submit" id="submit-button"><span>Test Transaction</span></button>
</form>
var form = document.querySelector('#payment-form');
braintree.dropin.create({
authorization: '{{ client_token }}',
container: '#bt-dropin',
paypal: {
flow: 'vault'
}
}, function (createErr, instance) {
form.addEventListener('submit', function (event) {
event.preventDefault();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Error', err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
urls.py
url(r'^checkout/$',
BraintreePaymentProcessFormView.as_view(),
name='checkout_braintree'),
views.py
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return reverse_lazy('my_shopping_cart')
else:
print(result.errors)
messages.success(self.request, 'An error occured while processing the payment')
return super().form_invalid(form)
布伦特里沙盒信用卡
形式
from django import forms
class BraintreeSaleForm(forms.Form):
payment_method_nonce = forms.CharField()
如果你有 form_valid
和 form_invalid
方法,你的 views.py 会更好,而且你必须像这样编辑你的 success_url
。
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
success_url = reverse_lazy('my_shopping_cart')
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
reverse_lazy('my_shopping_cart')
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self,form):
# Add action to invalid form phase
messages.success(self.request, 'An error occured while processing the payment')
return self.render_to_response(self.get_context_data(form=form))
我正在使用 Braintree 作为支付解决方案(沙盒)。当我使用填写表单时,它会将我重定向到同一页面。我的错误是表格没有得到处理。在 FormView 上只有 get_context_data()
被执行但没有 form_valid()
html
<script src="https://js.braintreegateway.com/web/dropin/1.6.1/js/dropin.min.js"></script>
<form action="{% url 'checkout_braintree' %}" method="POST" id="payment-form">
{% csrf_token %}
<h3>Method of Payment</h3>
<p>378282246310005</p>
<input type="hidden" id="nonce" name="payment_method_nonce" />
<div class="bt-drop-in-wrapper">
<div id="bt-dropin"></div>
</div>
<button class="button" type="submit" id="submit-button"><span>Test Transaction</span></button>
</form>
var form = document.querySelector('#payment-form');
braintree.dropin.create({
authorization: '{{ client_token }}',
container: '#bt-dropin',
paypal: {
flow: 'vault'
}
}, function (createErr, instance) {
form.addEventListener('submit', function (event) {
event.preventDefault();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Error', err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
urls.py
url(r'^checkout/$',
BraintreePaymentProcessFormView.as_view(),
name='checkout_braintree'),
views.py
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return reverse_lazy('my_shopping_cart')
else:
print(result.errors)
messages.success(self.request, 'An error occured while processing the payment')
return super().form_invalid(form)
布伦特里沙盒信用卡
形式
from django import forms
class BraintreeSaleForm(forms.Form):
payment_method_nonce = forms.CharField()
如果你有 form_valid
和 form_invalid
方法,你的 views.py 会更好,而且你必须像这样编辑你的 success_url
。
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
success_url = reverse_lazy('my_shopping_cart')
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
reverse_lazy('my_shopping_cart')
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self,form):
# Add action to invalid form phase
messages.success(self.request, 'An error occured while processing the payment')
return self.render_to_response(self.get_context_data(form=form))