歡迎關(guān)注我的公眾號:zx94_11
通過電子郵件共享帖子
創(chuàng)建表單
blog/forms.py
from django import formsclass EmailPostForm(forms.Form): name = forms.CharField(max_length=25) email = forms.EmailField() to = forms.EmailField() comments = forms.CharField(required=False, widget=forms.Textarea)
配置郵件服務(wù)器
mysite/settings.py
# Django將郵件輸出至Shell中陈轿,用于缺少SMTP服務(wù)器的應(yīng)用程序測試# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'# 正常的EMAIL配置EMAIL_HOST = 'smtp.163.com' # SMTP服務(wù)器主機(jī)EMAIL_HOST_USER = '你的163郵箱' # SMTP服務(wù)器用戶名EMAIL_HOST_PASSWORD = '你163郵箱對應(yīng)的密碼' # SMTP服務(wù)器密碼EMAIL_PORT = 25 # SMTP端口EMAIL_USE_TLS = False # 是否采用TLS安全連接# 在python manage.py shell進(jìn)行測試# from django.core.mail import send_mail## send_mail('Django mail', # 主題# 'This e-mail was sent with Django.', # 消息# 'zx490336534@163.com', # 發(fā)送者# ['490336534@qq.com'], # 接收者列表# fail_silently=False) # 如果郵件沒有被正確的發(fā)送,拋出一個(gè)異常昙楚。# # 如果正常發(fā)送 輸出結(jié)果為1
配置視圖
blog/views.py
from .forms import EmailPostFormfrom django.core.mail import send_maildef post_share(request, post_id): post = get_object_or_404(Post, id=post_id, status='published') sent = False if request.method == 'POST': form = EmailPostForm(request.POST) # POST請求會生成一個(gè)表單實(shí)例 if form.is_valid(): cd = form.cleaned_data post_url = request.build_absolute_uri(post.get_absolute_url()) subject = f'{cd["name"]} ({cd["email"]}) recommends you reding "{post.title}"' message = f'Read "{post.title}" at {post_url}\n\n{cd["name"]} comments:{cd["comments"]}' send_mail(subject, message, EMAIL_HOST_USER, [cd['to']]) sent = True else: form = EmailPostForm() # GET請求會給出一個(gè)空表單 return render(request, 'blog/post/share.html', {'post': post, 'form': form, 'sent': sent})
配置路由
blog/urls.py
path('<int:post_id>/share/', views.post_share, name='post_share')
顯示模版中的視圖
blog/templates/blog/post/share.html
{% 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 action="." method="post"> {{ form.as_p }} {% csrf_token %} <input type="submit" value="Send e-mail"> </form> {% endif %}{% endblock %}
{% csrf_token %}
包含了自動(dòng)生成的令牌泞边,避免跨站點(diǎn)請求偽造(CSRF)
在blog/templates/blog/post/detail.html
{% endblock %}前面增加發(fā)送鏈接
<p> <a href="{% url 'blog:post_share' post.id %}"> Share this post </a></p>
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">帖子詳情</figcaption>
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">發(fā)送測試</figcaption>
結(jié)果查看
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">頁面展示結(jié)果</figcaption>
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">實(shí)際結(jié)果1</figcaption>
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">實(shí)際結(jié)果2</figcaption>