recipient_list 没有在 Django 中获取电子邮件地址

recipient_list isn't getting the email address in django

我在 Django 中发送 smtp 电子邮件时遇到问题。我尝试使用用户模型获取电子邮件地址 => get_email = User.objects.filter(is_admin=True).values_list('email') 但是当我将它传递给 recipient_list 时,它找不到电子邮件地址。这是我的 views.py:

from django.shortcuts import render
from feedbacks.models import Feedback
from django.contrib import messages
from django.core.mail import send_mail
from django.conf import settings

from django.contrib.auth import get_user_model

User = get_user_model()


def feedback(request):
    status = Feedback.objects.all()
    get_email = User.objects.filter(is_admin=True).values_list('email')
    print(get_email)

    if request.method == 'POST':
        name = request.POST["name"]
        student_id = request.POST["student_id"]
        adviser_init = request.POST["adviser_init"]
        phone = request.POST["phone"]
        email = request.POST["email"]
        issues = request.POST["issues"]

        obj = Feedback.objects.create(name=name, student_id=student_id, 
                                adviser_init=adviser_init, phone=phone,
                                  email=email, issues=issues)
        obj.save()
        try:
            subject = 'Student Feedback'
            message = "Mail from Student ID:" + student_id + "\nIssue:" + issues + ""
            email_from = settings.EMAIL_HOST_USER
            send_mail(subject, message, email_from, [get_email])
            messages.success(request, 'Your issue has been sent to our admin. '
                                  'Check feedback status for update. Thank You!')
        except:
            messages.error(request, 'Feedback Saved but not send to admin.')
    context = {
        'status': status
    }
    return render(request, 'feedback/feedback.html', context)

收件人列表必须是 list 对象,而不是查询集或我认为的任何其他类似对象的数组。因此,将以下修改添加到您的 get_mail 变量中:


    mail_qs = User.objects.filter(is_admin=True).values_list('email', flat=True)
    get_mail = list(mail_qs)

如果查询集不为空,应该可以工作。