2016-10-22 20 views
1

私はdjangoシグナルから自分のウェブサイトアドレスを使って電子メールを送信しようとしています。私はこの質問が見つかりました:Django:シグナルハンドラ内でドメイン名を取得する方法

AttributeError: 'NoneType' object has no attribute 'get_host'

は私のコードでget_current_siteから来ている:

https://stackoverflow.com/a/15521046/2385132をし、受け入れ答えに助言されたように進行しますが、そのコードを使用しているとき、私はこのエラーを取得しています
@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のバージョン(おそらくあなたの場合)には

答えて

3

、ドメインは常にSITE_IDがあなたの設定で定義されていない場合は、要求から取らです。 1.8 Djangoのバージョンで導入されたthis変更を参照してください:

Changed in Django 1.8:

This function will now lookup the current site based on request.get_host() if the SITE_ID setting is not defined.

だから、あなたの場合request=Noneにあなたはsites frameworkを有効にする必要がありますが、現在のサイト/ドメインのエントリとSite表に右のインスタンスを指してSITE_ID設定、これを試してみてください:)

+0

実際に私はdjango v1.9.7 :)を使用しています。私はあなたの答えと一緒に行くと思うので、ありがとう。 –

+0

hehe ..ええ、私は私の答えを更新しました – trinchet

関連する問題