게시물 세부정보:

[django toolBox] 장고 mailchimp 연동하기

[django toolBox] 장고 mailchimp 연결하기

 

1.html 입력 폼

 
                        <h4>뉴스레터</h4>
                        <form action="/newsletter/" method="POST" class="footer-newslatter">
                        {% csrf_token %}
                            <input type="email" placeholder="이메일 입력" name="newsemail">
                            <button type="submit" class="site-btn">구독하기</button>
                            <p>*최신 기술 뉴스를 받아보세요</p>
                        </form>

 

2.Views

from django.conf import settings
from mailchimp_marketing import Client
from mailchimp_marketing.api_client import ApiClientError
   

# Mailchimp Settings
api_key = settings.MAILCHIMP_API_KEY
server = settings.MAILCHIMP_DATA_CENTER
list_id = settings.MAILCHIMP_EMAIL_LIST_ID

# 메일침프 구독 
def subscribe(newsemail):
    

    mailchimp = Client()
    mailchimp.set_config({
        "api_key"api_key,
        "server"server,
    })

    member_info = {
        "email_address"newsemail,
        "status""subscribed",
    }

    try:
        response = mailchimp.lists.add_list_member(list_idmember_info)
        print("response: {}".format(response))
    except ApiClientError as error:
        print("An exception occurred: {}".format(error.text))

def newsletter(request):
    if request.method == "POST":
        newsemail = request.POST['newsemail']
        subscribe(newsemail)
        messages.success(request"뉴스레터 이메일 구독에 감사합니다!"# 구독감사메세지

    return redirect('courses:home')

 

3.연결

from django.urls import path

from users.views import newsletter

urlpatterns = [    
    path('newsletter/'newslettername='newsletter'),
]

 

4.장고 메일침프 설치

pip install mailchimp-marketing

 

5.settings.py

# MAILCHIMP CREDENTIALS
MAILCHIMP_API_KEY = ""
MAILCHIMP_DATA_CENTER = ""
MAILCHIMP_EMAIL_LIST_ID = ""

목록으로