-
-
Save maxwellamaral/b860668e46c45f02f11156a4643daf18 to your computer and use it in GitHub Desktop.
Enviando emails atraves do Django, utilizando formulário
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 4. Criar link no template html --> | |
<p> | |
<a href="{% url 'blog:post_share' post.id %}">Share this post</a> | |
</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2. Criar formulário de envio | |
from django import forms | |
class EmailPostForm(forms.Form): | |
name = forms.CharField(max_length=25) | |
email = forms.EmailField() | |
to = forms.EmailField() | |
comments = forms.CharField(required=False, widget=forms.Textarea) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Configurar email | |
EMAIL_HOST = 'smtp.gmail.com' | |
EMAIL_HOST_USER = '[email protected]' | |
EMAIL_HOST_PASSWORD = 'senha' | |
EMAIL_PORT = '587' | |
EMAIL_USE_TLS = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 3. Criar URL | |
from django.urls import path | |
#[...] | |
urlpatterns = [ | |
#[...] | |
path('<int:post_id>/share/', views.post_share, name='post_share') | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 5. Criar regra de negócio na view | |
def post_share(request, post_id): | |
# Recebe o objeto atraves do id do modelo | |
post = get_object_or_404(Post, id=post_id, status='published') | |
sent = False | |
# Se o formulário foi enviado via POST do formulário | |
if request.method == 'POST': | |
# Recebe a classe criada no passo 2 | |
form = EmailPostForm(request.POST) | |
# Se o formulário é válido | |
if form.is_valid(): | |
# Recebe os dados do formulário criado no passo 2 e submetido pelo any_template.html no passo 4 | |
cd = form.cleaned_data | |
# (opcional) pega a URL do Post do blog | |
post_url = request.build_absolute_uri(post.get_absolute_url()) | |
# pega os dados do formulário supracitado | |
subject = f'{cd["name"]} recommends you read {post.title}' | |
message = f'Read {post.title} at {post_url}\n\n' \ | |
f'{cd["name"]} \'s comments: {cd["comments"]}' | |
# envia o e-mail | |
if send_mail(subject, message, '[email protected]', [cd['to']]): | |
sent = True | |
# Se não foi enviado dados atraves do formulário, mas houve um simples acesso à página | |
else: | |
# Recebe o formulário em branco criado no passo 2 | |
form = EmailPostForm() | |
# Retorna a página com os dados do contexto | |
return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment