我将代码复制并粘贴到我的IDE(TextWrangler)中。现在,当我尝试运行我的代码时,我收到大量关于缩进和无效语法的随机错误。
在我复制&之前,代码完美无缺。将它从一个Django视图粘贴到另一个视图中。我几乎100%确定代码在我的新视图中仍然是正确的,但是,每次运行时我都会收到大量与缩进和无效语法相关的错误(甚至多行注释,如'''触发“无效语法第234行“错误。
我已经尝试将IDE切换到崇高,甚至退回所有缩进,然后重新使用它们无济于事。每次我在一行上修复“错误”时,会在另一行上创建一个新错误。
我的代码如下,请让我知道有关如何修复的任何想法。
@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 } )
答案 0 :(得分:2)
这就是您应该使用软标签而不是硬标签的原因。您至少有一行混合它们(请查看c.save()
行),查看代码的编辑版本。将IDE设置更改为始终使用空格或制表符(如果您还没有),我建议使用空格。
有关如何在sublime中查看空白以查找有问题的制表符,请参阅this question。
答案 1 :(得分:2)
以下是我的编辑器中代码的两个屏幕截图,其中有标签(设置为4),空格字符显示为偏红色。正如你所看到的那样,它在很多方面都包含了两者的相当大杂烩。 Python非常敏感,并且保持一致非常重要。这通常通过将编辑器配置为始终将制表符转换为 n 空白字符(反之亦然,但前者通常是首选)来处理。
要解决您的问题,请使用单一方法重新缩进所有内容。我的编辑器还有一个convert-tabs-to-spaces命令,可以先用它来简化任务。