嗨,我是Django的新手,只是从文档中学习。我只是用django发送电子邮件https://docs.djangoproject.com/en/2.1/topics/email/。 我想在加入消息中发送电子邮件,并希望发送带有驾驶员ID的网址来预订汽车。如何发送带有他的ID的网址。例如我在员工网站上有4个用户。我们公司的员工点击链接向注册用户发送电子邮件。单击发送电子邮件后,如何在我的URL中获取驱动程序ID,并在加入消息中向他发送一个链接,其中他的ID在URL中。
Views.py
def rentacar_carapp_approve(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 = driver_email
if to_email is not None:
join_message = """Please hit the link and book a car "url with his id " """
send_mail(subject=subject, from_email=from_email, recipient_list=[to_email], message=join_message,
fail_silently=False)
print("email sent")
except Driver.DoesNotExist:
print("Driver doesn't exists")
return HttpResponseRedirect('/mega-admin/rentacar/carapp-manager/')
Url.py
url(r'^drivers/(?P<driver_id>\d+)/$',rent_views.book,name='book'),
答案 0 :(得分:0)
你可以试试这个:
if request.is_secure():
protocol = 'https://'
else:
protocol = 'http://'
host = request.get_host()
url = protocol + host + reverse('url_name', kwargs={user_id: user_id})
然后在 send email() 中将 url 与您的邮件正文连接起来。