Html django管理员向用户发送电子邮件

Html django管理员向用户发送电子邮件,html,django,Html,Django,\管理员 @admin.register(ParentsProfile) class ParentsProfile(admin.ModelAdmin): list_display = ('Father_Email','Fathers_Firstname' , 'Fathers_Middle_Initial', 'Fathers_Lastname', 'Request') ordering = ('Request',) search_fields = ('Request',

\管理员

@admin.register(ParentsProfile)
class ParentsProfile(admin.ModelAdmin):
    list_display = ('Father_Email','Fathers_Firstname' , 'Fathers_Middle_Initial', 'Fathers_Lastname', 'Request')
    ordering = ('Request',)
    search_fields = ('Request',)
    actions = ['Send_Email','Send_Email_Disapproved']
    def Send_Email(self, request, queryset):
        html_content = "Your Registration has been approved.\n\nPlease use this %s as your username and %s as your password. \nYou may now start enrolling your student using this link https://...../Plogin_form/ \n\n\n REGISTRAR "
        for profile in queryset:
            send_mail(subject="Invite", message=html_content %(profile.Father_Email,profile.Parent_Password), from_email=settings.EMAIL_HOST_USER,
                       recipient_list=[profile.Father_Email])  # use your email function here
    def Send_Email_Disapproved(self, request, queryset):
        # the below can be modified according to your application.
        # queryset will hold the instances of your model
        for profile in queryset:
            send_mail(subject="Invite", message="Our Apology,\n\n Your Registration has been Disapproved " + profile.Father_Email + "\n\n\n REGISTRAR" + "", from_email=settings.EMAIL_HOST_USER,
                       recipient_list=[profile.Father_Email])
我有这个代码发送电子邮件给用户,我如何将我的html_内容转换成html?这样我就可以为用户设计我的消息了


它像这样发送给用户gmail,

您可以使用
django.template.loader.render\u to\u string

首先需要将模板存储在模板文件夹中,例如:

<!-- your_app/templates/my_template.html -->
<h1>hello, {{ user.first_name }}</h1>

Doc:

html\u content=render\u to\u string('my\u template.html',{foo':'bar')你从哪里得到这个“bar”?这里的想法是,你可以将上下文变量传递到你的电子邮件中,比如用户名/名字等,检查更新的答案在你的例子中,用
{user.username}
替换第一个
%s
,但是您无法从数据库中获取密码,因为它会被散列(这是一个很好的度量)
html_content = render_to_string('my_template.html', {
    'user': User.objects.get(username="some_username")
})