Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework email verification using generic view

I am done with the registration as you can see. Now I want to send email verification so users can confirm. So once a user register, he/she gets a mail for confirmation.

how do I send email verification using "ListCreateAPIView"?

Do I need a third party package?

Can someone help me out? Thanks

Here is my view

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

My serializer.py

class UserSerializer(serializers.ModelSerializer):
    email = serializers.EmailField(required=True, validators=[UniqueValidator(queryset=User.objects.all())])
    username = serializers.CharField(required=True, validators=[UniqueValidator(queryset=User.objects.all())])
    password = serializers.CharField(min_length=8, style={'input_type': 'password', 'placeholder': 'Password'})


    def create(self, validated_data):
        user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])
        return user

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')

like image 953
techstack Avatar asked Feb 11 '19 00:02

techstack


1 Answers

from django.core.mail import send_mail
class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def perform_create(self, serializer):
        created_object = serializer.save()
        send_mail('Subject here','Here is the message.','[email protected]', 
            [created_object.email],  fail_silently=False,)

send email when object has been created

for create a link and verify follow this tutorial doc

like image 174
paras chauhan Avatar answered Sep 20 '22 12:09

paras chauhan