TypeError: send_mail() missing 1 required positional argument: 'self'

Hey guys,

I would appreciate your help.

I’m using the book “Practical Django 2 and Channels 2 Building…” by Federico Marani.
I’m getting this error when I run my test:
“TypeError: send_mail() missing 1 required positional argument: ‘self’”

Here are the files:


EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"


from django import forms
from django.core.mail import send_mail
import logging

logger = logging.getLogger(__name__)
class ContactForm(forms.Form):
    name = forms.CharField(label='Your Name', max_length=100)
    message = forms.CharField(max_length=600, widget=forms.Textarea)
def send_mail(self):
    logger.info("Sending mail to customer service")
    message = "From: {0}\n{1}".format(
        "Site message",


from django.test import TestCase
from django.core import mail
from main import forms

class TestForm(TestCase):
    def test_valid_contact_us_form_sends_email(self):
        form = forms.ContactForm({
            'name': "Luke Skywalker",
            'message': "Hi there"})

*** I’m getting an error at line 14 ***

        with self.assertLogs('main.forms', level='INFO') as cm:
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Site message')
        self.assertGreaterEqual(len(cm.output), 1)
    def test_invalid_contact_us_form(self):
        form = forms.ContactForm({'message': "Hi there"})

Side note: When posting code or error messages here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your original post for this.)

I have tried to keep the indentation the same as you have posted, but I’m not sure something wasn’t distorted along the way. The problem here is that the def send_mail doesn’t show as properly indented under ContactForm, but I have no way to tell if it’s that way in your code or a copy/paste/edit error.

Thank you so very much, Ken.

It wasn’t indented correctly. It works now. :smile: