thismatters / django-mailinglist

Django package for administering outgoing mailing lists to subscribers.
GNU Affero General Public License v3.0
9 stars 1 forks source link

Help with a new simple form #7

Closed wildernessfamily closed 10 months ago

wildernessfamily commented 10 months ago

Hi, I happen to see your comment on another Django package and thought I would give it a try. Really impressed how well coded and well documented the package is. It was really simple to set up on my new Django site. Thank you for creating this awesome package!

I'm having one problem. I added a newsletter subscription form in the footer of the site. It only has one input field for the email address. If someone visits the site but dosn't want to create an account but does want to keep up to date with our newsletter this simple form allows them to.

The form I have:

<form action="{% url 'newsletter:footer-newsletter-subscribe' %}" method="post"

The url.py I have:

path('footer-newsletter-subscribe/', views.footerNewsletterSubscribe, name='footer-newsletter-subscribe'),

The views.py is where I'm having a problem. I created this view:

def footerNewsletterSubscribe(request):
    email = request.POST.get('email')

    mailing_list_slug = 'general'

    mailing_list = MailingList.objects.get(slug=mailing_list_slug)

    subscription_service = SubscriptionService()

    subscription_service.subscribe(user=None,
                                   mailing_list=mailing_list,
                                   force_confirm=False)

    return redirect(reverse(
        'home:home'))  # Adjust 'home' to the name of your home page URL

When I try to submit an email address, I receive this error: Exception Value: 'NoneType' object has no attribute 'email'

I tried this and it works. The double confirmation email is sent, 'BUT' a new user is created without the first name and last name. I don't want a new user to be created and I'm not sure how to get around this. Any guidance would be greatly appreciated.:

@require_POST
def footerNewsletterSubscribe(request):

    email = request.POST.get('email')

    mailing_list_slug = 'general'

    mailing_list = MailingList.objects.get(slug=mailing_list_slug)

    subscription_service = SubscriptionService()
    user = subscription_service.create_user(email=email, first_name='', last_name='')

    subscription_service.subscribe(user=user, mailing_list=mailing_list, force_confirm=False)

    return redirect(reverse('home:home'))
thismatters commented 10 months ago

Hi, thanks for your interest in the package!

It seems that you found the create_user service method which is exactly the correct method for what you're doing. It seems like you might want to have a separate db table for storing your mailinglist subscriptions and your site users. To do this you will need to create a model for holding basic user-like data and reference that model in the setting MAILINGLIST_USER_MODEL. You may also find it useful to customize the functionality for creating the user by providing your own hookset and referencing it in the MAILINGLIST_HOOKSET setting. (links go to the sample project)

wildernessfamily commented 10 months ago

I created a new model and then set the MAILINGLIST_USER_MODEL and does exactly what I was trying to accomplish. Thank you!