我复制粘贴了工作代码到IDE - 结果Python报了很多错误
我把代码复制粘贴到我的开发环境(TextWrangler)里。现在当我尝试运行代码时,出现了很多关于缩进和无效语法的随机错误。
在我从一个Django视图复制到另一个视图之前,代码运行得非常好。我几乎可以肯定在新的视图里代码还是正确的,但每次运行时都会出现很多与缩进和无效语法有关的错误(甚至像'''这样的多行注释也会触发“第234行无效语法”的错误)。
我尝试过换开发环境到sublime,甚至把所有的缩进都退格删除,然后重新缩进,但都没有用。每次我修复一行的“错误”,另一行又会出现新的错误。
我的代码在下面,请告诉我有什么想法可以解决这个问题。
@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 } )
2 个回答
2
这就是为什么你应该使用软制表符而不是硬制表符的原因。你的代码中至少有一行混用了这两种制表符(看看包含c.save()
的那一行),可以在你代码的编辑版本中查看。请更改你的IDE设置,始终使用空格或制表符(如果你还没有这样做的话),我推荐使用空格。
想了解如何在Sublime中查看空白字符,以找到问题的制表符,可以参考这个问题。
3
这里有几张你代码的截图,显示在我的编辑器里,制表符(设置为4个空格)和空格字符用红色标出。你可以看到,很多行里混用了这两种格式。Python对空白字符非常敏感,所以保持一致性非常重要。通常可以通过设置编辑器,让它总是把制表符转换成n个空格(或者反过来,但前者通常更受欢迎)。
要解决你的问题,重新调整缩进,使用一种方法来处理。我的编辑器还有一个将制表符转换为空格的命令,可以先用这个来简化任务。