gpt4 book ai didi

python - 选项卡错误: inconsistent use of tabs and spaces in indentation - Integration MailChimp Django

转载 作者:行者123 更新时间:2023-12-01 07:51:27 24 4
gpt4 key购买 nike

我刚刚使用 AjaxMailchimp 集成到我的 Django 项目中。电子邮件地址字段工作正常,我能够在我的 Mailchimp 列表和数据库中获取电子邮件。现在,当我尝试添加一个字段,让用户将他的名字添加到表单中时,我不断收到如下错误,例如: from .utils import SendSubscribeMail
文件“C:\Users\locq\Desktop\Coding\SEARCH_APP\venv\src\search\utils.py”,第 8 行
self.fname = fname
^
TabError:缩进中制表符和空格的使用不一致
..

那么如何添加 FNAME 字段并在提交时将数据发送到 mailchimp?

这是我的代码:

在我的 Settings.p 中,我在底部添加了我的 Mailchimp 凭据,如下所示:

MAILCHIMP_API_KEY = '767ba************-us6'
MAILCHIMP_SUBSCRIBE_LIST_ID = '45c*******'

我创建了一个 utils.py 文件来使用 Threading、Mailchimp 包并从设置中导入我的凭据:

import threading
import mailchimp
from django.conf import settings

class SendSubscribeMail(object):
def __init__(self, email, fname):
self.email = email
self.fname = fname
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()

def run(self):
API_KEY = settings.MAILCHIMP_API_KEY
LIST_ID = settings.MAILCHIMP_SUBSCRIBE_LIST_ID
api = mailchimp.Mailchimp(API_KEY)
try:
api.lists.subscribe(LIST_ID, {'email': self.email}, merge_vars={'FNAME': self.fname}, double_optin=False)
except:
return False

自定义 Def 来获取输入名称:

def subscribe(request):
if request.method == 'POST':
fname = request.POST['fname_id']
Subscribe.objects.create(fname_id = fname)
email = request.POST['email_id']
email_qs = Subscribe.objects.filter(email_id = email)
if email_qs.exists():
data = {"status" : "404"}
return JsonResponse(data)
else:
Subscribe.objects.create(email_id = email)
SendSubscribeMail(email, fname) # Send the Mail, Class available in utils.py
return HttpResponse("/")

我用来在数据库的订阅表中添加电子邮件(和 fname)的 models.py:

class Subscribe(models.Model):
email_id = models.EmailField()
fname_id = models.CharField(max_length=255, blank=True, null=True, default='')
timestamp = models.DateTimeField(auto_now=True)

def __str__(self):
return self.email_id

我的 html 表单:

<div class="signup subs-form">

<div class="header">
<p>Sign Up For Beta</p>
</div>
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
</div>

<form method="POST" id="subscribe" class="subs-form">
{% csrf_token %}
<div>
<input type="email" class="button-" id="email" name="email_id" placeholder="E-mail">
<div class="input">
<input type="text" class="button-" id="fname" name="fname_id" placeholder="First Name">
<input type="submit" class="button-" id="submit" value="Join">
</div>
</div>
</form>

</div>

最后,这是我页面底部的 Ajax 代码:

  $('#subscribe').submit(function(e){
e.preventDefault();
var email_id = $("#email").val();
if(email_id){
var csrfmiddlewaretoken = csrftoken;
var fname_id = $("#fname").val();
var email_data = {"email_id": email_id,
"fname_id": fname_id,
"csrfmiddlewaretoken" : csrfmiddlewaretoken};
$.ajax({
type : 'POST',
url : '/subscribe/',
data : email_data,
success : function(response){
$('#email').val('');
if(response.status == "404"){
alert("This Email is already been subscribed!");
}
else{
alert("Thank you for Subscribing! Please Check your Email to Confirm the Subscription");
}
},
error: function(response) {
alert("Sorry Something went Wrong");
$('#email').val('');
}
});
return false;
}
else{
alert("Please provide correct email!");
}
});


function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();

function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
})

我怎样才能实现这个目标?

最佳答案

根据 python 文档,python 不能混合制表符和空格进行缩进,并且根据 PEP-8,每个缩进级别应使用四个空格。当我尝试这样做时,这是真的:

>>> for i in range(20):
'indent with tabs'
'indent with spaces'

SyntaxError: inconsistent use of tabs and spaces in indentation

如果您有一个 python IDE,不仅仅是将它们写入记事本或 TextEdit,它还应该将每个制表符转换为四个空格,或者(不推荐,因为 PEP-8)每 4 个四个空格转换为一个制表符。

编辑:您可以使用 Ctrl+H 组合查找/替换每个制表符 4 个空格(这不会影响您的实际代码;字符串中的制表符应为 '\t ')

关于python - 选项卡错误: inconsistent use of tabs and spaces in indentation - Integration MailChimp Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56193730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com