Sending email picking up data FK Django

Asked

Viewed 246 times

1

I have group and profile, in my register the person chooses the group and this group has the responsavel_id, I want to pick up and send a confirmation email to the responsible of that group that the user chose

py.models

class Grupo(models.Model):

    grupo = models.CharField(max_length=255)
    divisao = models.ForeignKey(Divisao)
    responsavel = models.ForeignKey(User)

    @property
    def get_responsavel(self):
        return self.responsavel

    def __unicode__(self):
        return self.grupo


class Perfil(User):
    telefone = models.CharField(max_length=255)
    ramal = models.CharField(max_length=255)
    data_inicio = models.DateField(default=datetime.now, blank=True)
    data_fim = models.DateField(null=True, blank=True)
    e_responsavel = models.BooleanField(default=False)
    vinculo = models.ForeignKey(Vinculo)
    grupos = models.ForeignKey(Grupo)
    divisao = models.ForeignKey(Divisao) 

py views.

def cadastro(request):
        if request.method == 'POST':
            form = UserForm(request.POST)
            formulario = form.save()

        if form.is_valid():
            formulario.set_password(form.cleaned_data['password'])
            formulario.save()

            def email(responsavel_id):
                # Envio de email
                current_site = get_current_site(request)
                message = render_to_string('acc_active_email.html', {
                    'user': formulario,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(formulario.pk)),
                    'token': account_activation_token.make_token(formulario),
                })
                mail_subject = 'Novo usuario no sistema'
                group = Grupo.objects.get(pk=responsavel_id)
                responsavel = group.get_responsavel()
                print ("ss")
                responsavel.email
                to_email = responsavel.email
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()

            return HttpResponseRedirect('/cadastro/login/')

    else:
        form = UserForm()


    return render(request, 'cadastro.html', {
        'form': form,

    })
  • I don’t understand what the problem is.

  • I have a registration page and the user chooses which group he is in. I need that when he clicks to send an email to the responsible of that group so that it allows the user entry, as I do?

  • Okay, that’s what you want to do, but is that the code you posted is what? An attempt to do it? If so, what’s the problem? Is there a mistake? Which one? And what would that function be email defined in the middle of the code?

  • Yes, it is an attempt, with this code I was able to send the email, but only taking the email that the user filled and not the email of the responsible according to the group that was chosen

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.