我是新手,只是看到django发送邮件的文档。我希望在看到驱动程序的详细信息后向驱动程序发送电子邮件,如果他们符合条件,我希望他们使用他们在注册到我的应用程序时提交的电子邮件地址发送电子邮件。请告诉我如何发送电子邮件。我已经设置了settings.py电子邮件主机用户的所有需要,以发送电子邮件在其他应用程序上的工作。但无法在此应用上运行。我如何获取驾驶员电子邮件并从我们公司的员工发送电子邮件。
Views.py
@csrf_protect
def rentacar_carapp_sendmail(request):
if request.POST:
try:
args['driver'] = driver = Driver.objects.get(id=request.POST.get('driver_id'))
subject = "Please Register Your Car"
from_email = settings.EMAIL_HOST_USER
to_email = carapp.get('car_app_driver_id.driver_email')
join_message = """thankyou we will contact you later """
send_mail(subject=subject, from_email=from_email, recipient_list=[to_email], message=join_message,
fail_silently=False)
driver.save()
except:
pass
return HttpResponseRedirect('/mega-admin/rentacar/driver-manager/')
Models.py
class Driver(models.Model):
class Meta():
db_table = "driver"
verbose_name = "Driver"
verbose_name_plural = "Drivers"
ordering = ['driver_firstname', 'driver_lastname']
driver_firstname = models.CharField(
max_length=64,
blank=False,
null=False
)
driver_lastname = models.CharField(
max_length=64,
blank=False,
null=False
)
driver_email = models.EmailField(
blank=False
)
答案 0 :(得分:3)
阅读此链接,这应该可以解决您的问题:How to work with ajax request with Django
小示例代码:
我的html:
<input type="text" name="email" placeholder="emailaddress">
<a class="button" type="submit" id="hire_car" value="{{ customer_id }}">Submit button for the customer</a>
Ajax调用:
<script>
$("#hire_car").on('click', function () {
var customer = ($(this).attr('value'));
$.ajax({
type: 'GET',
url: '{% host_url 'rentacar_carapp_sendmail' host 'your_host_name' %}',
data: {customer: customer},
dataType: 'json',
success: function (data) {
alert('Success');
}
});
});
</script>
如果您不了解Ajax调用中的host_url,请阅读documentation。
urls.py
url(r'^rentacar_carapp_sendmail$', views.rentacar_carapp_sendmail, name='rentacar_carapp_sendmail'),
发送邮件视图:
def rentacar_carapp_sendmail(request):
customer = Driver.objects.get(id=request.POST.get('customer'))
customer_email = customer.email
# Send mail to customer
subject = "Please Register Your Car"
message = "thankyou we will contact you later"
email_from = "your email" \
email_to = (customer_email)
send_mail(subject, message, email_from, email_to, fail_silently=False)