Skip to content

Commit

Permalink
Added Django forms to share posts via email
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent c2689f9 commit eb6a0fe
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 1 deletion.
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions Module02/mysite/blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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)
2 changes: 2 additions & 0 deletions Module02/mysite/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ def get_absolute_url(self):
self.publish.day,
self.slug,
])



5 changes: 5 additions & 0 deletions Module02/mysite/blog/templates/blog/post/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ <h1>{{ post.title }}</h1>
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
<p>
<a href="{% url "blog:post_share" post.id %}">
Share this post
</a>
</p>
{% endblock %}
17 changes: 17 additions & 0 deletions Module02/mysite/blog/templates/blog/post/share.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "blog/base.html" %}
{% block title %}Share a Post{% endblock %}
{% block content %}
{% if sent %}
<h1>E-mail successfully sent</h1>
<p>
"{{ post.title }}" was successfully sent to {{ form.cleaned_data.to }}.
</p>
{% else %}
<h1>Share "{{ post.title }}" by e-mail</h1>
<form method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" value="Send e-mail">
</form>
{% endif %}
{% endblock %}
1 change: 1 addition & 0 deletions Module02/mysite/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# path('', views.post_list, name='post_list'),
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
]
23 changes: 22 additions & 1 deletion Module02/mysite/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
from .forms import EmailPostForm
from django.core.mail import send_mail


# def post_list(request):
Expand Down Expand Up @@ -35,4 +37,23 @@ def post_detail(request, year, month, day, post):
publish__month=month,
publish__day=day
)
return render(request, 'blog/post/detail.html', {'post': post})
return render(request, 'blog/post/detail.html', {'post': post})

def post_share(request, post_id):
# Retrieve post by id
post = get_object_or_404(Post, id=post_id, status=Post.Status.PUBLISHED)
sent = False
if request.method == 'POST':
# Form was submitted
form = EmailPostForm(request.POST)
if form.is_valid():
# Form fields passed validation
cd = form.cleaned_data
post_url = request.build_absolute_uri(post.get_absolute_url())
subject = f"{cd['name']} recommends you read {post.title}"
message = f"Read {post.title} at {post_url} \n {cd['name']}\'s comments: {cd['comments']}"
send_mail(subject, message, 'alaaali151992@gmail.com', [cd['to']])
sent = True
else:
form = EmailPostForm()
return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent})
Binary file modified Module02/mysite/mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions Module02/mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,12 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Email server configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_USER = 'your_gmail_account'
EMAIL_HOST_PASSWORD = 'your_added_app_password'

0 comments on commit eb6a0fe

Please sign in to comment.