I am trying to get CC to accept multiple email addresses but I do not seem to get it to work as it always says
Enter a valid email address. I've searched for solutions, heard the new field
MultiValueField but I also do not know how to use this in
forms.py.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is my
forms.py code:
from django import forms
# from django.forms.fields import MultiValueField
from django.forms.widgets import EmailInput, TextInput
class ComposeForm(forms.Form):
email_to = forms.EmailField(label="To", widget=EmailInput(attrs={"size": 76}))
email_cc = forms.EmailField(
label="CC",
required=False,
widget=EmailInput(attrs={"size": 76, "multiple": True}),
)
email_subject = forms.CharField(
required=False, widget=TextInput(attrs={"placeholder": "Subject", "size": 76})
)
email_message = forms.CharField(
required=True, label="", widget=forms.Textarea(attrs={"rows": 19, "cols": 78})
)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from django.shortcuts import render
from .forms import ComposeForm
def email_template(request):
if request.method == "GET":
form = ComposeForm()
else:
form = ComposeForm(request.POST)
if form.is_valid():
print(form)
email_to = form.cleaned_data["email_to"]
email_cc = form.cleaned_data["email_cc"]
email_subject = form.cleaned_data["email_subject"]
email_message = form.cleaned_data["email_message"]
print("Emails:", email_cc)
else:
print("DEBUG:", form.errors)
return render(request, "email_template.html", {"form": form})
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And this is my email_template.html HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<form method="POST">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please tell me whether it's possible to have multiple email addresses in
email_cc aka
CC field in Django or do I have to use frontend frameworks for this one job or start using one for better practice (get used to using frameworks to build frontend side)?
Please advise.
Regards,
Kristen