0

I want to ask for guidance on how to do this Django email notification https://docs.djangoproject.com/en/1.8/topics/email/#send-mail

I have a basic task form and option to assign it to someone, when the form is saved I want to send an email notification to the assigned user.

Job/task models.py

class Job(models.Model):
    completed = models.BooleanField(default=False)
    task_name = models.CharField(max_length=80, blank=False)
    description = models.CharField(max_length=80, blank=False)
    is_important = models.BooleanField(default=False)
    completion_date = models.DateField(blank=True, null=True)
    assign_to = models.ForeignKey(User, blank=True, null=True)
    comments = models.TextField(blank=True)

    def __unicode__(self):
        return self.task_name

Job/task view.py

@login_required
def job(request):

    if request.method == 'POST':

        form = JobForm(request.POST)

        if form.is_valid():
            job_record = form.save(commit=False)
            job_record = form.save(commit=False)
            job_record.user = request.user
            job_record.save()
            return redirect('jobs:list')
    else:
        form = JobForm()

    return render(request, 'jobs/form.html', {'form': form})
Anentropic
  • 32,188
  • 12
  • 99
  • 147
nope
  • 197
  • 1
  • 2
  • 14
  • 2
    OK, what's your issue? You have the save call there, and the documentation about how to send an email, so where are you having problems? – Daniel Roseman May 12 '15 at 11:05
  • I am quite new to python and django, the thing is I am not sure how and where to start, that is why I am asking for guidance / help / explanation. – nope May 12 '15 at 11:22
  • Did you configure settings.py ? You may find this useful : https://www.youtube.com/watch?v=51mmqf5a0Ss – Sylvain Biehler May 12 '15 at 11:36
  • But you don't need to start: most of the code is already there. It's really hard to know where your trouble is. What is preventing you simply putting in that `send_mail` call after `job_record.save()`? – Daniel Roseman May 12 '15 at 11:43
  • Yes I'have got that, thank you, but how would I get the email of the user that the task was assigned to? – nope May 12 '15 at 12:29

1 Answers1

0

You are nearly there:

@login_required
def job(request):

    form = JobForm(request.POST or None)

    if form.is_valid():
        job_record = form.save(commit=False)
        job_record.assign_to = request.user
        job_record.save()
        send_mail(
            subject="subject",
            message="message",
            from_email="from@myserver.com",
            recipient_list=[job_record.assign_to.email]
        )
        return redirect('jobs:list')

    return render(request, 'jobs/form.html', {'form': form})
Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • You can find details of [Django's user model](https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#django.contrib.auth.models.User) in the docs, the rest is in your own code. One good way to explore unfamiliar objects is to `pip install ipython` and then use Django's `./manage.py shell` command to open an ipython console and experiment with instantiating your models and see the methods and attributes available – Anentropic May 12 '15 at 13:15