私はdjangoシグナルから自分のウェブサイトアドレスを使って電子メールを送信しようとしています。私はこの質問が見つかりました:Django:シグナルハンドラ内でドメイン名を取得する方法
AttributeError: 'NoneType' object has no attribute 'get_host'
は私のコードでget_current_site
から来ている:
@receiver(post_save, sender=MyModel)
def post_obj_save(sender, instance: MyModel, **kwargs):
def _get_html(obj: MyModel):
return render_to_string('confirmation_email.html', _get_context(obj))
def _get_context(obj: MyModel):
current_site = get_current_site(request=None)
domain = current_site.domain
action = reverse('obj_activation', request=None, format=None, kwargs={})
url = '{protocol}://{domain}/{action}'.format(protocol=PROTOCOL, domain=domain, action=action)
return {
'header': _('Thank you for registering with ASDF.'),
'prompt': _('In order to be able to log in into ASDF administrator panel, you have to activate your account using'),
'link_name': _('this link'),
'activation_url': url
}
send_mail(
_('ASDF account activation'),
_get_html(instance),
EMAIL_FROM,
[obj.owner.email],
fail_silently=False,
)
質問は次のとおりです。信号で私の見解の完全なURLを取得するにはどうすればよいですか?
Btw。私はdjango-rest-framework
を使用しています。最近Djangoのバージョン(おそらくあなたの場合)には
実際に私はdjango v1.9.7 :)を使用しています。私はあなたの答えと一緒に行くと思うので、ありがとう。 –
hehe ..ええ、私は私の答えを更新しました – trinchet