Python Django rest框架:sendgird电子邮件,带附件,不带仅限型号的文件字段,用于打开文件,并通过发送按钮发送电子邮件
我是Django Rest框架的新手。Python Django rest框架:sendgird电子邮件,带附件,不带仅限型号的文件字段,用于打开文件,并通过发送按钮发送电子邮件,python,django,django-rest-framework,sendgrid,Python,Django,Django Rest Framework,Sendgrid,我是Django Rest框架的新手。 我正在尝试发送带有附件的电子邮件 这是我的密码 model.py class EmailModel(models.Model): upload_file = models.FileField(upload_to='location/location/files', blank=False) class Meta: verbose_name = 'Applicant CSV Upload' verbose_n
我正在尝试发送带有附件的电子邮件 这是我的密码 model.py
class EmailModel(models.Model):
upload_file = models.FileField(upload_to='location/location/files', blank=False)
class Meta:
verbose_name = 'Applicant CSV Upload'
verbose_name_plural = 'Applicant CSV Upload'
@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
class Meta:
model = EmailModel
def send_email():
email = EmailMessage(
'Title',
('abc', 'abc@gmail.com', '123123123'),
'abc@gmail.com',
['abc@gmail.com']
)
email.attach_file(EmailViewSet.upload_file)
email.send()
class EmailViewSet(viewsets.ModelViewSet):
queryset = EmailModel.objects.all()
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
response = super(EmailViewSet, self).create(request, *args, **kwargs)
send_email() # sending mail
data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
serializer = self.get_serializer(data=data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
# Attach file
# with open(attachment, 'rb') as file:
# message.attachments = [
# (attachment, file.read(), 'application/pdf')
# ]
return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = EmailModel
fields = ('upload_file',)
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'
admin.py
class EmailModel(models.Model):
upload_file = models.FileField(upload_to='location/location/files', blank=False)
class Meta:
verbose_name = 'Applicant CSV Upload'
verbose_name_plural = 'Applicant CSV Upload'
@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
class Meta:
model = EmailModel
def send_email():
email = EmailMessage(
'Title',
('abc', 'abc@gmail.com', '123123123'),
'abc@gmail.com',
['abc@gmail.com']
)
email.attach_file(EmailViewSet.upload_file)
email.send()
class EmailViewSet(viewsets.ModelViewSet):
queryset = EmailModel.objects.all()
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
response = super(EmailViewSet, self).create(request, *args, **kwargs)
send_email() # sending mail
data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
serializer = self.get_serializer(data=data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
# Attach file
# with open(attachment, 'rb') as file:
# message.attachments = [
# (attachment, file.read(), 'application/pdf')
# ]
return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = EmailModel
fields = ('upload_file',)
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'
View.py
class EmailModel(models.Model):
upload_file = models.FileField(upload_to='location/location/files', blank=False)
class Meta:
verbose_name = 'Applicant CSV Upload'
verbose_name_plural = 'Applicant CSV Upload'
@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
class Meta:
model = EmailModel
def send_email():
email = EmailMessage(
'Title',
('abc', 'abc@gmail.com', '123123123'),
'abc@gmail.com',
['abc@gmail.com']
)
email.attach_file(EmailViewSet.upload_file)
email.send()
class EmailViewSet(viewsets.ModelViewSet):
queryset = EmailModel.objects.all()
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
response = super(EmailViewSet, self).create(request, *args, **kwargs)
send_email() # sending mail
data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
serializer = self.get_serializer(data=data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
# Attach file
# with open(attachment, 'rb') as file:
# message.attachments = [
# (attachment, file.read(), 'application/pdf')
# ]
return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = EmailModel
fields = ('upload_file',)
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'
序列化程序.py
class EmailModel(models.Model):
upload_file = models.FileField(upload_to='location/location/files', blank=False)
class Meta:
verbose_name = 'Applicant CSV Upload'
verbose_name_plural = 'Applicant CSV Upload'
@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
class Meta:
model = EmailModel
def send_email():
email = EmailMessage(
'Title',
('abc', 'abc@gmail.com', '123123123'),
'abc@gmail.com',
['abc@gmail.com']
)
email.attach_file(EmailViewSet.upload_file)
email.send()
class EmailViewSet(viewsets.ModelViewSet):
queryset = EmailModel.objects.all()
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
response = super(EmailViewSet, self).create(request, *args, **kwargs)
send_email() # sending mail
data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
serializer = self.get_serializer(data=data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
# Attach file
# with open(attachment, 'rb') as file:
# message.attachments = [
# (attachment, file.read(), 'application/pdf')
# ]
return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = EmailModel
fields = ('upload_file',)
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'
设置.py
class EmailModel(models.Model):
upload_file = models.FileField(upload_to='location/location/files', blank=False)
class Meta:
verbose_name = 'Applicant CSV Upload'
verbose_name_plural = 'Applicant CSV Upload'
@admin.register(EmailModel)
class EmailAdmin(admin.ModelAdmin):
class Meta:
model = EmailModel
def send_email():
email = EmailMessage(
'Title',
('abc', 'abc@gmail.com', '123123123'),
'abc@gmail.com',
['abc@gmail.com']
)
email.attach_file(EmailViewSet.upload_file)
email.send()
class EmailViewSet(viewsets.ModelViewSet):
queryset = EmailModel.objects.all()
serializer_class = EmailSerializer
def create(self, request, *args, **kwargs):
send_mail(' Test Subject here', 'Test here is the message.', 'abc@gmail.com', ['abc@gmail.com'], fail_silently=False)
response = super(EmailViewSet, self).create(request, *args, **kwargs)
send_email() # sending mail
data = [{'location': request.data.get('location'), 'image': file} for file in request.FILES.getlist('image')]
serializer = self.get_serializer(data=data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = EmailMessage(subject='Applicant data', body='PFA', from_email='abc@gmail.com',
to='abc@gmail.com', bcc='abc@gmail.com', connection=None,
attachments=data, headers=self.data, cc='abc@gmail.com', reply_to=None)
# Attach file
# with open(attachment, 'rb') as file:
# message.attachments = [
# (attachment, file.read(), 'application/pdf')
# ]
return response, message.send(), Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class EmailSerializer(serializers.ModelSerializer):
class Meta:
model = EmailModel
fields = ('upload_file',)
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'here i am using my sendgrid api key directy' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'here i am using same gmail id on which i have created my send grid account'
在view.py和serializer.py中
我已经提到了我尝试过的每一种发送电子邮件的方法,这就是为什么它如此混乱的原因。这些方法都不起作用。
即使是create
方法也根本不调用
这是显示在我的api管理员我想改变保存按钮文本发送
您可以在不需要模型的地方创建自定义管理页面。解决了这个问题
现在,当您创建自定义页面时,在视图中,您可以简单地使用sendgrid提供的python API并执行您想要实现的任何操作。以下是相同的示例。如果您想发送带有附件的邮件,您已经在此处询问了更多详细信息谢谢。Vaibhav Singhwhy my Overrided create不在View.py和serializer.py中工作您附加的屏幕截图是您的ModelAdmin视图,它不会调用您的Emailviewset。我不在django中工作,我在django Rest框架中工作。