0

There are quite a few youtube or instructions online but I could not find anything to apply this to blog/create. What I mean by that, I would like to have email to be sent automatically to the guy who create a new post.

Here is the blog/views.py below;

from django.core.mail import send_mail

class PostCreate(LoginRequiredMixin, CreateView):
    model = Post
    fields = [
        'title', 'content', 'head_image', 'category', 'tags'
    ]

    def form_valid(self, form):
        current_user = self.request.user
        if current_user.is_authenticated:
            form.instance.author = current_user
            return super(type(self), self).form_valid(form)
        else:
            return redirect('/blog/') 

I did setup settings.py like below:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'example@gmail.com' # my email address I put
EMAIL_HOST_PASSWORD = '****************' # the password I put
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

I have a project to do right now... I don't have much time to review and study again... I am such a beginner... looking forward to having some help from here

Peter Kam
  • 95
  • 8
  • Have you tried using a post-save signal? – Scratch'N'Purr May 25 '21 at 07:15
  • I googled abou the post-save signal but looks like it's too complecated and heavy to me... I think I should put some code at blog/views.py, especially at 'class PostCreate'. because I would like to have an email to be sent after creating a new pot. thanks anyway for your help. I think I will review about the post-save signal part someday – Peter Kam May 25 '21 at 07:43
  • If you don't want to use the post-save signal, then the other option is overriding the `save()` method for the model. Be sure to check for [`self._state.adding`](https://docs.djangoproject.com/en/3.2/ref/models/instances/#state) which is a boolean flag that determines if it's a new post or not. – Scratch'N'Purr May 25 '21 at 07:55
  • I installed signals but it keep saying 'no module signals and ask me to install that packages... I did several times but still same issue – Peter Kam May 25 '21 at 08:12
  • Signals is part of the django package. There shouldn't be an external package required to install. – Scratch'N'Purr May 25 '21 at 08:44
  • I am tring to import blog.signals.... maybe I am doing wrong... I don't know – Peter Kam May 25 '21 at 09:06
  • Why you do not send it on form_valid? – Jarno Lahtinen May 25 '21 at 09:40
  • Scratch'N'Purr> I made spelling mistake... I created 'signal.py' and used name, 'signals'... always tiny mistake ocurred – Peter Kam May 25 '21 at 09:54
  • Jarno Lahtinen >> please give an advise how to do that thaknks – Peter Kam May 25 '21 at 10:15
  • '''' @receiver(signals.post_save, sender=Post) def send_mail(sender, instance, created, **kwargs): print('signal send') subject = "Thank you" message = Post.objects.get( ) send_mail(subject, 'message', '', ['I put my email address here'], fail_silently=False, ) ```` – Peter Kam May 25 '21 at 10:17
  • Post.objects.get( ) for this part I don't know what parametar, kwargs I have to put... keep error . I did try pk, id .... but 'TypeError at /blog/create/ – Peter Kam May 25 '21 at 10:20

0 Answers0