从 Django 表单向导发送邮件
Send mail from django form wizard
我有任何模型和视图生成一个表单向导,一旦用户提交最后一个表单,我希望应用程序发送一封确认邮件,当前用户的邮件是在原生 Django 模型中,我如何访问他的电子邮件?以及我如何也可以访问 "curso" 名称?感谢您的帮助。
models.py
class Datos_Basicos (models.Model):
user = models.OneToOneField(User)
tipo_identificacion = models.CharField(max_length=25)
identificacion = models.CharField(max_length=20,primary_key='True')
telefono = models.CharField(max_length=10)
celular = models.CharField(max_length=11)
class Curso(models.Model):
curso = models.CharField(max_length=15)
fk_programa = models.ForeignKey(Programa, verbose_name="programa")
class Inscripcion(models.Model):
fecha = models.DateField(default = datetime.datetime.now)
fk_subsede = models.ForeignKey(Subsede, verbose_name="sede")
fk_curso = models.ForeignKey(Curso, verbose_name="curso")
class Inscrito(models.Model):
fk_datos_basicos = models.OneToOneField(Datos_Basicos, verbose_name="datos basicos")
fk_inscripcion = models.OneToOneField(Inscripcion, verbose_name="curso", null=False)
forms.py
class Datos_basicos_form(forms.ModelForm):
tipo_identificacion = forms.ChoiceField(choices=IDENTIFICACION_CHOICES)
class Meta:
model = Datos_Basicos
fields = ['tipo_identificacion', 'identificacion', 'telefono', 'celular']
class Inscrito_form(forms.Form):
genero = forms.ChoiceField(choices=GENEROS_CHOICES)
fecha_nacimiento = forms.DateField(widget=extras.SelectDateWidget(years=ANIOS_CHOICES))
grupo_etnico = forms.ChoiceField(choices=GRUPO_ETNICO_CHOICES)
condicion = forms.ModelChoiceField(queryset=Condicion.objects.all())
condicion_medica = forms.CharField(max_length=200)
eps = forms.CharField(max_length=100)
espacio_geografico = forms.ChoiceField(choices=ESPACIO_GEOGRAFICO_CHOICES)
barrio = forms.ModelChoiceField(queryset=Barrio.objects.order_by('barrio'))
direccion = forms.CharField(max_length=50)
jornada_estudio = forms.ChoiceField(choices=JORNADA_ESTUDIO_CHOICES)
class Inscripcion_form(forms.ModelForm):
class Meta:
model = Inscripcion
exclude = ['fecha']
views.py
from formtools.wizard.views import SessionWizardView
class InscritoWizard(SessionWizardView):
template_name = "inscripcion.html"
def done(self, form_list, **kwargs):
forms_data = self.process_form_data(form_list)
forms_data[1]['user'] = self.request.user ### current user
forms_data[2]['edad'] = hoy.year - forms_data[2]['fecha_nacimiento'].year - ((hoy.month, hoy.day) < (forms_data[2]['fecha_nacimiento'].month, forms_data[2]['fecha_nacimiento'].day))
inscripcion = Inscripcion.objects.create(**forms_data[0])
datos_basicos = Datos_Basicos.objects.create(**forms_data[1])
forms_data[2]['fk_datos_basicos'] = datos_basicos
forms_data[2]['fk_inscripcion'] = inscripcion
Inscrito.objects.create(**forms_data[2])
--->send_mail('Subject here', 'Here is the message.','from@example.com',['to@example.com'], fail_silently=False)
return render_to_response('gracias.html', {'forms_data': forms_data})
def process_form_data(self, form_list):
data = [form.cleaned_data for form in form_list]
return data
在表单向导方法中,您可以使用 self.request.user
访问登录用户,因此您可以使用 self.request.user.email
.
获取他们的电子邮件
我有任何模型和视图生成一个表单向导,一旦用户提交最后一个表单,我希望应用程序发送一封确认邮件,当前用户的邮件是在原生 Django 模型中,我如何访问他的电子邮件?以及我如何也可以访问 "curso" 名称?感谢您的帮助。
models.py
class Datos_Basicos (models.Model):
user = models.OneToOneField(User)
tipo_identificacion = models.CharField(max_length=25)
identificacion = models.CharField(max_length=20,primary_key='True')
telefono = models.CharField(max_length=10)
celular = models.CharField(max_length=11)
class Curso(models.Model):
curso = models.CharField(max_length=15)
fk_programa = models.ForeignKey(Programa, verbose_name="programa")
class Inscripcion(models.Model):
fecha = models.DateField(default = datetime.datetime.now)
fk_subsede = models.ForeignKey(Subsede, verbose_name="sede")
fk_curso = models.ForeignKey(Curso, verbose_name="curso")
class Inscrito(models.Model):
fk_datos_basicos = models.OneToOneField(Datos_Basicos, verbose_name="datos basicos")
fk_inscripcion = models.OneToOneField(Inscripcion, verbose_name="curso", null=False)
forms.py
class Datos_basicos_form(forms.ModelForm):
tipo_identificacion = forms.ChoiceField(choices=IDENTIFICACION_CHOICES)
class Meta:
model = Datos_Basicos
fields = ['tipo_identificacion', 'identificacion', 'telefono', 'celular']
class Inscrito_form(forms.Form):
genero = forms.ChoiceField(choices=GENEROS_CHOICES)
fecha_nacimiento = forms.DateField(widget=extras.SelectDateWidget(years=ANIOS_CHOICES))
grupo_etnico = forms.ChoiceField(choices=GRUPO_ETNICO_CHOICES)
condicion = forms.ModelChoiceField(queryset=Condicion.objects.all())
condicion_medica = forms.CharField(max_length=200)
eps = forms.CharField(max_length=100)
espacio_geografico = forms.ChoiceField(choices=ESPACIO_GEOGRAFICO_CHOICES)
barrio = forms.ModelChoiceField(queryset=Barrio.objects.order_by('barrio'))
direccion = forms.CharField(max_length=50)
jornada_estudio = forms.ChoiceField(choices=JORNADA_ESTUDIO_CHOICES)
class Inscripcion_form(forms.ModelForm):
class Meta:
model = Inscripcion
exclude = ['fecha']
views.py
from formtools.wizard.views import SessionWizardView
class InscritoWizard(SessionWizardView):
template_name = "inscripcion.html"
def done(self, form_list, **kwargs):
forms_data = self.process_form_data(form_list)
forms_data[1]['user'] = self.request.user ### current user
forms_data[2]['edad'] = hoy.year - forms_data[2]['fecha_nacimiento'].year - ((hoy.month, hoy.day) < (forms_data[2]['fecha_nacimiento'].month, forms_data[2]['fecha_nacimiento'].day))
inscripcion = Inscripcion.objects.create(**forms_data[0])
datos_basicos = Datos_Basicos.objects.create(**forms_data[1])
forms_data[2]['fk_datos_basicos'] = datos_basicos
forms_data[2]['fk_inscripcion'] = inscripcion
Inscrito.objects.create(**forms_data[2])
--->send_mail('Subject here', 'Here is the message.','from@example.com',['to@example.com'], fail_silently=False)
return render_to_response('gracias.html', {'forms_data': forms_data})
def process_form_data(self, form_list):
data = [form.cleaned_data for form in form_list]
return data
在表单向导方法中,您可以使用 self.request.user
访问登录用户,因此您可以使用 self.request.user.email
.