robert
robert

Reputation: 31

Django and Stripe MultiValueDictKeyError at /checkout/ "'stripeToken'"

    views.py
    from django.conf import settings
    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    import stripe


    stripe.api_key = settings.STRIPE_SECRET_KEY

    # Create your views here.
    @login_required
    def checkout(request):
        publishKey = settings.STRIPE_PUBLISHABLE_KEY
        if request.method == 'POST':
            token = request.POST['stripeToken']
                try:
                stripe.Charge.create(
                    amount=1000,
                    currency="usd",
                    card=token,


                    description="Charge for [email protected]"
    )
            except stripe.CardError:
                # The card has been declined
                pass
            # Create the charge on Stripe's servers - this will charge the user's card

        context = {'publishKey': publishKey

                    }
        template = 'checkout.html'
        return render(request, template, context)



When i run my views.py gives me this error -    MultiValueDictKeyError at /checkout/
"'stripeToken'"

here is my checkout.py
MultiValueDictKeyError at /checkout/
"'stripeToken'"

that is my checkout.py please help out i would appreciate it. Im making a website to take payments eventually but when i run it i get that error i think its coming from the views.py but am not to sure would like to get some feed back from some djangonites or pythoners thanks

Upvotes: 1

Views: 2219

Answers (4)

Muhuri.JSON
Muhuri.JSON

Reputation: 173

Refresh your publishable and secret api keys, delete all test data then try apply print the token on your terminal to see if its actually coming through :

if 'stripeToken' in request.POST:   
        print(request.POST['stripeToken'])

Upvotes: 0

Shubham Aggarwal
Shubham Aggarwal

Reputation: 363

I had the same problem and the cause of the problem was in checkout.html. I was using this

 Stripe.setPublishableKey({{ publishkey }});

Instead of

Stripe.setPublishableKey('{{ publishkey }}');

Correcting it solved the error for me

Upvotes: 0

7stud
7stud

Reputation: 48649

>>> from django.utils.datastructures import MultiValueDict as MVD
>>> data = MVD({'name': ['John'], 'email': ['[email protected]']})
>>> data['name']
'John'
>>> data['email']
'[email protected]'

This class [MultiValueDict] exists to solve the irritating problem raised by cgi.parse_qs, which returns a list for every key, even though most Web forms submit single name-value pairs.


cgi.parse_qs: Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.


Now with a non-existent key:

>>> data['apple']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/7stud/.virtualenvs/django186p34/lib/python3.4/site-packages/django/utils/datastructures.py", line 322, in __getitem__
    raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "'apple'"

See how the last line says ...MultiValueDictKeyError: "'apple'"? See how the key 'apple' doesn't exist in the MultiValueDict created in the django console above?

See how you wrote:

request.POST['stripeToken']

and you got a similar error?? request.POST is a MultiValueDict which contains the key/value pairs from the POST data. The error is saying that "stripeToken" is not a key in the MultiValueDict, which means there was no key/value pair in the POST data with the key 'stripeToken'.

If the data came from an html form, that means there was no form input element with its name attribute set to "stripeToken", e.g.

<input type="text" name="stripeToken">

Upvotes: 0

Rikka
Rikka

Reputation: 1049

Django raises this error when the requested key, stripeToken in this example, does not exist in request.POST. You may want to check your POST form data for your request.

Upvotes: 1

Related Questions