Create email template with the click of a button

411 views
Skip to first unread message

Smiley

unread,
Mar 21, 2021, 4:31:11 PM3/21/21
to Django users
Hello,

I am new to Django and recently finished the Polls app tutorial in Django site. After that, I read what to do next and Django is a big fan of learning by doing so I went online and looked for beginner projects to do to learn web development.

I am planning to create a site to automate or manually send out emails with a lot more customizations to learn both front-end and back-end. After starting to do it I realized that a lot of work has to be done on client-side so here's the question.

Do I have to use Javascript to create an email template when user clicks the button create email or can this be done in Python / Django? 

My github repository for this project: https://github.com/SmileyBoy321/Django-Email-Sender

The progress is slow as I am learning and also working full-time on non-dev job. I am sacrificing some fragments of my life to make time for coding.

Please advise, thank you.

Kind regards,
Kristen

Sebastian Jung

unread,
Mar 21, 2021, 6:33:42 PM3/21/21
to django...@googlegroups.com
Hello,

This is dir Django a relativ easy Task. You create a Form with fields Like to,CC,BCC,subject,Text message, HTML Message and Render These Form. Then User enter all Data and when it submit you get the Post Data And submit IT with djangos Sendmail https://docs.djangoproject.com/en/3.1/topics/email/

I Hope that i can Help you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7043c96-9976-4354-aaa9-b1edd29172f6n%40googlegroups.com.

Kristen

unread,
Mar 22, 2021, 8:11:48 AM3/22/21
to django...@googlegroups.com
Hello,

Correct me if I'm wrong. I understand that creating the template is possible with django but I have one question. 

Is it possible to create that email template with the click of a button? For example, in Gmail there's a button "compose". When you click that button it creates an email template with to, CV, text area to write text to and other stuff. 

Right now my implementation is via javascript where it creates that email template dynamically on button click hut I myself wanted to know if it's possible to do this with Django.

Let me know if you have any questions.

Kind regard

jdsleppy

unread,
Mar 22, 2021, 10:30:00 AM3/22/21
to Django users
It sounds like you want the user to click a button and have a form appear where the user can compose an email.  The simplest implementation might be to make that button a link (<a>) to a page like /email/compose that renders the form. No javascript needed there. Gmail uses javascript to render their form on top of the inbox because they don't want you to have to leave the inbox page. You probably don't have that requirement.

Let me know if that works for you.

Cheers,
Joel

Kristen

unread,
Mar 22, 2021, 10:47:03 AM3/22/21
to django...@googlegroups.com
Hello,

Correct. I want to the user to click a button and have a form appear where the user can compose an email.

Gmail uses javascript to render their form on top of the inbox because they don't want you to have to leave the inbox page. You probably don't have that requirement.

Let me know if that works for you.

I don't really want to redirect the user elsewhere. I would really like to do the Gmail way because I like it and thought maybe it's possible with Django.

Nonetheless, I will try out your idea because my curiosity wants to know whether it's worth the effort to do it the Gmail way or go easier route way by doing it just as you explained.

Let me know what you think of it, the Gmail way idea. 

Kind regards

Smiley

unread,
Mar 23, 2021, 4:11:26 PM3/23/21
to Django users
Hello,

Thank you for your help guys.

I've went the Django form route instead of doing this in JS.

I'll create the form dynamically with template and form.

Kind regards

Sebastian Jung

unread,
Mar 23, 2021, 4:24:01 PM3/23/21
to django...@googlegroups.com
I have implement it as a bootstrap modal where a Form with fields are shown. When User click submit the Page load New but If you don't want that Page is load New you need a Ajax jquery submit to django

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Smiley

unread,
Apr 15, 2021, 4:21:44 PM4/15/21
to Django users
Hello,

I have another problem. 

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})
    )

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

And this is my views.py code:

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

Ryan Nowakowski

unread,
Apr 17, 2021, 1:53:08 PM4/17/21
to django...@googlegroups.com
I've had a similar issue in the past. With the latest version of Django pretty much each database back in now supports JSONField. So I created a custom field that inherits from JSON field to take a string of comma delimited values, split them by comma, validate each value individually, then store the whole thing in the database as a JSON list.

In your case you'll use the email validator used by the EmailField to validate each value.

Sebastian Jung

unread,
Apr 17, 2021, 2:01:18 PM4/17/21
to django...@googlegroups.com
Hello,

Emailfield is nothing other then a normal InputField but later with a special validator. I would take InputField then write your own Validator where you split input commasparated an validate every address with a regex.


Regards

Ryan Nowakowski

unread,
Apr 17, 2021, 6:13:22 PM4/17/21
to django...@googlegroups.com
Sorry for the weird wording below. Speech to text isn't all is cracked up to be. Just one more point: the JSONField allows your to easily do queries on the individual email addresses if your need to.
Reply all
Reply to author
Forward
0 new messages