1

I copied and pasted code into my IDE (TextWrangler). Now when I try to run my code, I get a ton of random errors regarding indentation and invalid syntax.

The code worked perfectly before I copied & pasted it from one Django view into another. I'm almost 100% sure the code is still correct in my new view, however, every time it runs I'll get a ton of errors relating to indentation and invalid syntax (even multiline comments like ''' trigger an "invalid syntax on line 234" error.

I've tried switching IDE's over to sublime, and even backspacing all indentations and then retabbing them to no avail. Each time I fix an "error" on one line, a new error on another line is created.

My code is below, please let me know any thoughts on how to fix.

@require_POST   
def pay(request):


if request.method == 'POST':
    form = CustomerForm(request.POST)

    if form.is_valid(): 
    # If the form has been submitted...
    # All validation rules pass


        #get the customer by session'd customer_id
        c = get_object_or_404(Customer, pk = request.session['customer_id'])

        #assign shipping info from POST to the customer object
        c.first_name = request.POST['first_name']
        c.last_name = request.POST['last_name']
        c.street_address = request.POST['street_address']
        c.city = request.POST['city']
        c.state = request.POST['state']
        c.zip = request.POST['zip']

        #assign email info from POST to the customer object

        c.email_address = request.POST['email_address']

        stripe.api_key = REDACTED

        # Get the credit card details submitted by the form
        token = request.POST['stripeToken']


        #tries to save the newly added form data. 
        try:
            #save the new customer object's data
                c.save()

##########   THIS HANDLES CREATING A NEW STRIPE PAYMENT ################                    

    # Create a Customer 
        try:
            customer = stripe.Customer.create(
            card=token,
            plan="monthly",
            email= c.email_address)
            #need to save customer's id (ex: c.stripe_id = token.id)

            #if there's a token error   
        except stripe.error.InvalidRequestError, e:
            pass

        #if the card is declined by Stripe

        except stripe.error.CardError, e:
            body = e.json_body
            err  = body['error']

            print "Status is: %s" % e.http_status
            print "Type is: %s" % err['type']
            print "Code is: %s" % err['code']
            # param is '' in this case
            print "Param is: %s" % err['param']
            print "Message is: %s" % err['message']

        except stripe.error.AuthenticationError, e:
        # Authentication with Stripe's API failed
        # (maybe you changed API keys recently)
            pass

        except stripe.error.APIConnectionError, e:

        # Network communication with Stripe failed
            pass

        except stripe.error.StripeError, e:
        # Display a very generic error to the user, and maybe send
        # yourself an email
            pass

        except Exception, e:
        # Something else happened, completely unrelated to Stripe
            pass    

        return render(request, 'shipment/confirm.html', {'date' : 'April 15, 2014'})


            #passes the context to the template for confirming the customer's data
            #context = { 'email_address' : c.email_address, 'first_name' : c.first_name,
            #   'last_name' : c.last_name, 'street_address' : c.street_address, 
            #   'city' : c.city, 'state' : c.state, 'zip' : c.zip, }

            #return render(request, 'shipment/pay.html', context)

        #If there is a duplicate email it redirects the user back to the form with no error message.  




#If anything else happens, it redirects the user back to the form.      
else:
    form = CustomerForm() # An unbound form
    return render(request, 'shipment/createAccount.html', { 'form': form } )
MikeBrody
  • 317
  • 3
  • 14
  • 1
    This is why you should use soft tabs instead of hard tabs. You have at least one line that mixes them, looking at the `edit` version of your code. – roippi Mar 27 '14 at 15:20
  • `s// /`, and change your IDE settings to always use spaces or tabs (if you haven't already), I recommend [spaces](http://opensourcehacker.com/2012/05/13/never-use-hard-tabs/). – keyser Mar 27 '14 at 15:24
  • How can find which line is causing the problem and fix it? Also, is a soft tab equivalent to pressing the spacebar 4 times? – MikeBrody Mar 27 '14 at 15:28
  • See http://stackoverflow.com/questions/10153998/sublime-text-2-view-whitespace-characters for how to view whitespace in sublime to find the offending tab character. – chthonicdaemon Mar 27 '14 at 15:30
  • My money is on the line with c.save() – chthonicdaemon Mar 27 '14 at 15:32
  • I combined all the comments on this question into a CW answer, as all of them combined makes for a good answer for the question. – Steinar Lima Mar 27 '14 at 15:41
  • Are your first `if` and last `else` really at the top level, or are they indented under the `def(pay):` line? It's often hard to know if indentation errors are part of your actual code or just an artifact of copying into Stack Overflow's question box. – Blckknght Mar 27 '14 at 15:48

2 Answers2

3

Here's a couple of screenshots of your code in my editor with tabs (set to 4) and space characters shown in a reddish color. As you can see it contains quite a hodgepodge of the two on many lines. Python is very whitespace sensitive and it's important to be consistent. This is usually handled by configuring your editor to always convert tabs to n whitespace characters (or vice versa, but the former is often preferred).

To fix your problem, re-indent everything using a single method. My editor also has a convert-tabs-to-spaces command which could be used first to simplify the task somewhat.

first screenshot

second screenshot

martineau
  • 119,623
  • 25
  • 170
  • 301
  • I was going to try to fix your code myself but got confused with the two `try` statements that follow one another. It's certainly possible to nest them, but too unclear which `except` clauses went with which `try` -- if in fact that's what you intended. – martineau Mar 27 '14 at 16:30
  • Thanks very much! I downloaded Sublime and was able to fix everything as you suggested! – MikeBrody Apr 01 '14 at 05:48
2

This is why you should use soft tabs instead of hard tabs. You have at least one line that mixes them (check out the line with c.save()), looking at the edit version of your code. Change your IDE settings to always use spaces or tabs (if you haven't already), I recommend spaces.

See this question for how to view whitespace in sublime to find the offending tab character.

Community
  • 1
  • 1
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40