I have made an api for submitting a contact form in my application. Whenever the form gets submitted i want it to send that details to myself through email. Here is my code what i am using:
models.py
class Contact(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField(max_length=254)
phone = models.CharField(max_length=20)
message = models.TextField()
def __str__(self):
return self.name
class Meta:
db_table = 'contact'
views.py
from rest_framework.generics import (
CreateAPIView,
)
from .serializers import (
ContactCreateSerializer
)
class ContactCreateApiView(CreateAPIView):
queryset = Contact.objects.all()
serializer_class = ContactCreateSerializer
serializers.py
class ContactCreateSerializer(ModelSerializer):
class Meta:
model = Contact
fields = [
'name',
'email',
'phone',
'message',
]
def send_email(self):
name = self.cleaned_data.get('name')
from_email = self.cleaned_data.get('email')
phone = self.cleaned_data.get('phone')
subject = 'Contact Form Info from App'
message = self.cleaned_data.get('message')
to_email = settings.EMAIL_HOST_USER
context = {
'name': name,
'email': from_email,
'phone': phone,
'message': message,
}
plaintext = get_template('emails/contact_form_email.txt')
htmly = get_template('emails/contact_form_email.html')
text_content = plaintext.render(context)
html_content = htmly.render(context)
message = EmailMultiAlternatives(
subject=subject,
body=text_content,
from_email=from_email,
to=[to_email],
cc=[],
bcc=[]
)
message.attach_alternative(html_content, "text/html")
message.send()
def save(self, commit=True):
instance = super().save(commit)
self.send_email() # there you send the email when then model is saved in db
return instance
I tried this but it gives me error
save() takes 1 positional argument but 2 were given
What should i do?