'502 Bad Gateway during during user Django user registration on ubuntu,ngnix

I am working on a django project,the registration view works pretty fine on development on my localhost and heruko test server.But when I switch to production using Ubuntu and Ngnix.I get a 502 bad gateway error whenever a user tries to submit the registration form.Other forms e.g the login is perfect.The only problem is the registration part on production.

def registerView(request):
    form = UserCreationForm(request.POST or None)

    if form.is_valid():
        user = form.save(commit=False)
    
        user.is_active = False
        # user.set_unusable_password()
        user.save()
        current_site = get_current_site(request)
        mail_subject = 'Activate your account.'
        email_var = {
            'user' : user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
    
        }
        to_email = form.cleaned_data.get('email')
        from_email = settings.DEFAULT_FROM_EMAIL
        html_content = render_to_string('confirm_email.html',email_var)
        text_content = strip_tags(html_content)
        msg_html = render_to_string('confirm_email.html',email_var)
        send_mail(mail_subject,msg_html,from_email, [to_email],html_message=msg_html)
    
        return render(request,'email_redirect.html',{})
    context = {
        'form':form,
    }
    return render(request,'register.html',context)

class ActivateAccount(View):

    def get(self, request, uidb64, token, *args, **kwargs):
        try:
            uid = urlsafe_base64_decode(uidb64).decode()
            user = User.objects.get(pk=uid)

        except (TypeError, ValueError, OverflowError, User.DoesNotExist):
            user = None

        if user is not None and account_activation_token.check_token(user, token):
            user.is_active = True
            user.save()
            login(request, user)
            return render(request,'confirm_sucess.html',{})
        else:
            return render(request,'confirm_error.html',{})

forms.py

class UserCreationForm(forms.ModelForm):
    error_messages = {
        'password_mismatch': ('The two password fields didn’t match.'),
    }
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['firstname','email',]

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise ValidationError(self.error_messages['password_mismatch'],code='password_mismatch',)

        return password2

    def _post_clean(self):
        super()._post_clean()
        password = self.cleaned_data.get('password2')
        if password:
            try:
                password_validation.validate_password(password, self.instance)
            except ValidationError as error:
                self.add_error('password2', error)



    def clean_email(self):
        email = self.cleaned_data.get('email')
        email_qs = User.objects.filter(email=email)
        if email_qs.exists():
            raise forms.ValidationError("user with that email already exists")
        return email

    def clean_firstname(self):
        firstname = self.cleaned_data.get('firstname')
        name_qs = User.objects.filter(firstname=firstname)
        if name_qs.exists():
            raise forms.ValidationError("User with name alredy exists")
        return firstname

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])

        if commit:
            user.save()
        return user


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source