向注册到 django 应用程序的用户发送电子邮件
Posted
技术标签:
【中文标题】向注册到 django 应用程序的用户发送电子邮件【英文标题】:sending emails to users registered to a django application 【发布时间】:2020-04-08 13:55:41 【问题描述】:我正在做一个 django 项目。我是 django 项目,我有一个用于将用户注册到服务的表单。用户注册后,我应该可以向他们发送促销电子邮件。对于发送电子邮件本身,我使用mailchimp。到目前为止,这是我的代码
models.py
class RegistrationModel(models.Model):
firstName = models.CharField(max_length=200)
lastName = models.CharField(max_length=200)
email = models.EmailField(max_length=70)
phone = models.IntegerField()
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200, blank=True)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
pincode = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
def publish(self):
self.save()
def __str__(self):
return self.email
forms.py
from .models import RegistrationModel
class RegistrationForm(forms.ModelForm):
class Meta:
model = RegistrationModel
widgets =
'firstName': forms.TextInput(attrs='placeholder': 'First Name'),
'lastName': forms.TextInput(attrs='placeholder': 'Last Name'),
'email': forms.TextInput(attrs='placeholder': 'Email'),
'phone': forms.TextInput(attrs='placeholder': 'Phone Number'),
'address1': forms.TextInput(attrs='placeholder': 'Address1'),
'address2': forms.TextInput(attrs='placeholder': 'Address2'),
'city': forms.TextInput(attrs='placeholder': 'City'),
'state': forms.TextInput(attrs='placeholder': 'State'),
'pincode': forms.TextInput(attrs='placeholder': 'Pincode'),
fields = ('firstName', 'lastName', 'email', 'phone', 'address1', 'address2', 'city', 'state', 'pincode')
settings.py
MAILCHIMP_API_KEY = 'I have added the api key here'
MAILCHIMP_DATA_CENTER = 'I have added the data center here'
MAILCHIMP_EMAIL_LIST_ID = 'I have added the audience id here'
views.py
from django.conf import settings
from django.shortcuts import render, redirect
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from .models import RegistrationModel
from .forms import RegistrationForm
import requests, json
MAILCHIMP_API_KEY = settings.MAILCHIMP_API_KEY
MAILCHIMP_DATA_CENTER = settings.MAILCHIMP_DATA_CENTER
MAILCHIMP_EMAIL_LIST_ID = settings.MAILCHIMP_EMAIL_LIST_ID
api_url = f'https://MAILCHIMP_DATA_CENTER.api.mailchimp.com/3.0'
members_endpoint = f'api_url/lists/MAILCHIMP_EMAIL_LIST_ID/members'
def subscribe(email):
data =
"email_address": email,
"status": 'subscribed'
r = requests.post(
members_endpoint,
auth=("", MAILCHIMP_API_KEY),
data=json.dumps(data),
)
return r.status_code, r.json()
def index(request):
if request.method == "POST":
form = RegistrationForm(request.POST)
if form.is_valid():
RF_qs = RegistraionModel.objects.filter(email=form.instance.email)
if RF_qs.exists():
messages.info(request, "You are already subscribed")
else:
subscribe(form.instance.email)
form.save()
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
else:
form = RegistraionForm()
context = 'reg_form': form
return render(request, 'index.html', context)
urls.py
from sample_project import views
urlpatterns = [
path('', views.index, name='home'),
path('subscribe/', views.index, name='subscribe'),
path('about', views.about, name='about'),
path('contact', views.contact, name='contact'),
]
但是当我在应用程序中提交表单时,什么都没有发生,并且没有填充 mailchimp 中的受众。我不确定我做错了什么。
我正在关注这个video 来创建服务
【问题讨论】:
【参考方案1】:我认为您需要向 mailchimp 发送一个 json 有效负载,这还需要设置 content-type 标头和接受标头。为此,请更新subscribe
中的调用:
r = requests.post(
members_endpoint,
auth=("", MAILCHIMP_API_KEY),
json=data,
)
print(f'Response to mailchimp call [r.status_code]: r.json()')
【讨论】:
好像没有变化。我还在问题中添加了 urlpatterns。如果你需要看那个 您需要尝试捕获对 request.post 的响应并将其记录到控制台(使用记录器或打印)。 上例中添加。 终端中似乎没有打印任何内容。这可能是因为未调用订阅函数吗?还是网址不对之类的? 是的,这意味着该表单未被发现是有效的。 RegistrationForm 是否与VittalParivar
model 而不是 RegistrationModel
模型相关联?以上是关于向注册到 django 应用程序的用户发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章