**views.py**
from django.core.mail import send_mail
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render,get_object_or_404
from app.models import Post
from app.forms import EmailSendForm
def post_list_view(request):
post_list=Post.objects.all()
paginator=Paginator(post_list,3)
page_number=request.GET.get('page')
try:
post_list=paginator.page(page_number)
except PageNotAnInteger:
post_list=paginator.page(1)
except EmptyPage:
post_list=paginator.page(paginator.num_pages)
return render(request, 'post_list.html', {'post_list':post_list})
def post_detail_view(request,year,month,day,slug):
post=get_object_or_404(Post,slug=slug,
status='published',
publish__year=year,
publish__month=month,
publish__day=day,
)
return render(request, 'post_detail.html', {'post':post})
def SendMail(request,post_id):
post = get_object_or_404(Post,id=post_id,status='published')
sent=False
if request.method=='POST':
form=EmailSendForm(request.POST)
if form.is_valid():
cd=form.cleaned_data
subject='{}({})Please read message"{}"'.format(cd['name'],cd['email'],post.title)
post_url=request.build_absolute_uri(post.get_absolute_url())
message="Read Post at:\n {}\n\n{}\'s Comments:\n{}".format(post_url,cd['name'],cd['comments'])
send_mail(subject,message,'vpnraut@gmail.com',[cd['to']])
sent=True
else:
form = EmailSendForm()
return render(request, 'mail.html', {'sent':sent,'form':form, 'post':post})
**models.py**
from django.contrib.auth.models import User
from django.db import models
from Django.urls import reverse
from Django.utils import timezone
class CustomManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES=(('draft','Draft'),('published','Published'))
id=models.IntegerField(primary_key=True)
title=models.CharField(max_length=300)
slug=models.SlugField(max_length=300,unique_for_date='publish')
author=models.ForeignKey(User,related_name='post',on_delete=models.CASCADE)
body=models.TextField()
publish=models.DateTimeField(default=timezone.now)
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now=True)
status=models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')
objects=CustomManager()
class Meta:
ordering=('-publish', )
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post',args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
class Comment(models.Model):
post=models.ForeignKey(Post,related_name='comments',on_delete=models.CASCADE)
name=models.CharField(max_length=30)
email=models.EmailField()
body=models.TextField()
created=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(auto_now=True)
active=models.BooleanField(default=True)
class Meta:
ordering=('-created', )
def __str__(self):
return 'Commnted by {} on {}'.format(self.name,self.post)
**urls.py**
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.post_list_view),
path('<int:year>/<int:month>/<int:day>/<slug:slug>/',views.post_detail_view,name='post'),
path('(<int:post_id>\d+)/share/',views.SendMail, name='post_share'),]