I am creating a newsletter app and having issues with the redirect function working after a success message is displayed to the user.
I have already tried various approaches to refactoring -
[How to pass a message from HttpResponseRedirect in Django?
[https://github.com/zsiciarz/django-envelope/issues/28]
from django.conf import settings
from django.contrib import messages
from django.shortcuts import render, redirect, reverse
from django.core.mail import send_mail, EmailMultiAlternatives
from django.template.loader import get_template
""" Create views """
from .forms import CustomerSubscriptionForm
from .models import CustomerSubscriptionUser
def customer_subscribe(request):
form = CustomerSubscriptionForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
if CustomerSubscriptionUser.objects.filter(email=instance.email).exists():
messages.warning(request, "So you've already subscribe to us, so visit your email to see what we have in store for you!")
return redirect('/experiment/subscribe') - **This is where the problem is**
else:
instance.save()
messages.success(request, "We are excited that you're interested in being in our local dance community! We sent some more information to your email")
subject = "Thank you for joining our community"
from_email = settings.EMAIL_HOST_USER
to_email = [instance.email]
subscription_message = "Hi, LET'S DANCE. If you want to unsubscribe got to http://127.0.0.1:8000/experiment/unsubscribe/ "
send_mail(subject=subject, from_email=from_email, recipient_list=to_email, message=subscription_message, fail_silently=False)
context = {'form': form}
template = "experiment/subscribe.html"
return render(request, template, context)
Expected result - user is redirected to the form view after submitting requested information
Actual result - Only the message.warning value is shown