使用 Django 和 Mailchimp 的电子邮件订阅表单在部署后不起作用 (DigitalOcean)
Posted
技术标签:
【中文标题】使用 Django 和 Mailchimp 的电子邮件订阅表单在部署后不起作用 (DigitalOcean)【英文标题】:An email subscription form with Django and Mailchimp doesn't work after deployment (DigitalOcean) 【发布时间】:2021-03-20 09:14:14 【问题描述】:我用 Django 和 Mailchimp 构建了一个简单的电子邮件订阅表单。它在本地工作得很好,但是,在将其部署到 DigitalOcean 之后,它似乎不起作用。我以为它可能与数据库有关,但我确实在服务器上迁移并且没有收到任何错误(在服务器上使用 Ubuntu 20.04)。
希望有人有线索,可以帮助我解决这个问题。
这是表格: https://www.winoutt.io
views.py
from django.contrib import messages
from django.http import HttpResponseRedirect
# from django.conf import settings
from .models import Signup
from decouple import config
import json
import requests
# MAILCHIMP_API_KEY = settings.MAILCHIMP_API_KEY
# MAILCHIMP_DATA_CENTER = settings.MAILCHIMP_DATA_CENTER
# MAILCHIMP_EMAIL_LIST_ID = settings.MAILCHIMP_EMAIL_LIST_ID
MAILCHIMP_API_KEY = config("MAILCHIMP_API_KEY")
MAILCHIMP_DATA_CENTER = config("MAILCHIMP_DATA_CENTER")
MAILCHIMP_EMAIL_LIST_ID = config("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(email):
data = "email_address": email, "status": "subscribed"
req = requests.post(
members_endpoint, auth=("", MAILCHIMP_API_KEY), data=json.dumps(data)
)
return req.status_code, req.json()
def newsletter_email_list(request):
if request.method == "POST":
email = request.POST.get("email", None)
email_query = Signup.objects.filter(email=email)
if email:
if email_query.exists():
messages.info(request, "You are already on the waitlist.")
else:
try:
subscribe_email(email)
subscribed = Signup.objects.create(email=email)
subscribed.save()
messages.success(
request,
"Thank you! We're putting you on the waitlist. Bear with us.",
)
except:
messages.warning(request, "Something went wrong. Please try again.")
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
else:
messages.warning(request, "Please enter your email")
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
models.py
from django.db import models
class Signup(models.Model):
email = models.CharField(max_length=100)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.email
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from pages.views import custom_page_not_found_view, PageView
from about.views import AboutView
from privacy.views import PrivacyView
from newsletter.views import newsletter_email_list
handler404 = "pages.views.custom_page_not_found_view"
urlpatterns = [
path("jet/", include("jet.urls", "jet")),
path("admin/", admin.site.urls),
path("", PageView.as_view(), name="all_pages"),
# path("<slug>/", PostDetailView.as_view(), name="post_detail"),
path("tinymce/", include("tinymce.urls")),
path("about/", AboutView.as_view(), name="about"),
path("privacy-policy/", PrivacyView.as_view(), name="privacy"),
path("newsletter/subscribe/", newsletter_email_list, name="newsletter_subscribe"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
模板
<form action="% url 'newsletter_subscribe' %" class="flex items-end justify-center w-full mt-4" method="POST">
% csrf_token %
<input type="email" id="email" name="email" class="w-full px-3 py-1 mt-2 text-base leading-8 text-gray-100 transition-colors duration-200 ease-in-out border border-gray-700 rounded outline-none bg-blackLight focus:border-blue-500" placeholder="Your email" value=" email " required>
</div>
<button type="submit" class="inline-flex px-8 py-2 mt-4 mb-12 ml-4 text-lg text-white bg-blue-600 border-0 border-transparent rounded-lg shadow focus:outline-none hover:bg-blue-700" value="Send">Join</button>
</form>
.env
# Database Configurations
DB_ENGINE="django.db.backends.postgresql"
DB_NAME=""
DB_USER=""
DB_PASSWORD=""
DB_HOST="localhost"
【问题讨论】:
【参考方案1】:你们有关于 DigitalOcean 的数据库吗?如果您托管的是本地数据库,则需要更改为 sqlite 后端,或附加到云 RDBMS。
【讨论】:
我在本地使用了 postgresql,并在 DigitalOcean 上创建了一个新的用于生产。【参考方案2】:这似乎是一个许可问题。但是,我通过编辑 gunicorn.service 文件解决了这个问题。
我打开了 gunicorn.service 文件
sudo nano /etc/systemd/system/gunicorn.service
编辑前
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/djangoadmin/canny
ExecStart=/home/djangoadmin/.local/share/virtualenvs/canny-9cyzXi2O/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
config.wsgi:application
[Install]
WantedBy=multi-user.target
编辑后
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=djangoadmin
Group=djangoadmin
WorkingDirectory=/home/djangoadmin/canny
ExecStart=/home/djangoadmin/.local/share/virtualenvs/canny-9cyzXi2O/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
config.wsgi:application
[Install]
WantedBy=multi-user.target
我只是更改了“组”部分,以便它与我的用户匹配。
成功了。
【讨论】:
我还生成了一个新的 API 密钥,仅供生产使用。以上是关于使用 Django 和 Mailchimp 的电子邮件订阅表单在部署后不起作用 (DigitalOcean)的主要内容,如果未能解决你的问题,请参考以下文章
Mailchimp,通过 Django 发送邮件(),打开费率跟踪