0

I am implementing a stripe charge in my django ecommerce project. So In my views, I am trying to get the data from an input element in a form, then pass into a variable in the views.

Here is the Javascript that creates the input element

var form = document.getElementById('stripe-form');
    var hiddenInput = document.createElement('input');
    hiddenInput.setAttribute('type', 'hidden');
    hiddenInput.setAttribute('name', 'stripeToken');
    hiddenInput.setAttribute('value', token.id);
    
    form.appendChild(hiddenInput);
    
    // Submit the form
    form.submit()

So in the views.py, I have this:

token = form.cleaned_data.get('stripeToken')
save = form.cleaned_data.get('save')
use_default = form.cleaned_data.get('use_default')
 print(form.cleaned_data)

the print(form.cleaned_data) returned a dictionary

{'stripetoken':'', 'save': False, 'as_default': False}

stripetoken maps to the token variable, and as you can see its value is empty. The last two variables that maps to the last two keys in the dictionary returned values.

The token = form.cleaned_data.get('stripeToken') is supposed to get this hiddenInput.setAttribute('value', token.id); right?

When I tried getting the value of token.id, I got a response. I used Javascript alert(token.id) to do that, and I got a value displayed in my browser. So I don't know what exactly is wrong.

Can someone help me out on this one?

0

1 Answer 1

2

Your form.cleaned_data print shows you've defined a form field called stripetoken (all lower-case), not stripeToken (camelCased).

Your JavaScript code is sending stripeToken (camelCased), but since there isn't a field by that name in your form, Django ignores the "extraneous" data sent by the browser and the cleaned data just includes the default for the field (which I will assume you've set to be '').

Fix your backend code to spell it stripeToken, or your frontend code to spell it stripetoken.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks I have seen the problem. I can't believe I spent so much time trying to figure out this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.