2013-03-20 2 views
2

내 장고 프로젝트에서 신호를 썼습니다. 내 웹 사이트의 전체 경로와 함께 전자 메일을 보내려고합니다. 하지만 이렇게하려면 요청 객체를 신호 내부에 가져와야합니다. 어떻게 할 수 있습니까? 감사.장고 신호 요청 전체 경로

@receiver(pre_save, sender=AccessRequest) 
    def email_if_access_true(sender, instance, **kwargs): 
     #How can I get the full path of my website here? 
     pass 

답변

2

넣고 다음 코드 SITE_ID=1을 정의했다고 확인해야 템플리트에 필요한 컨텍스트 변수. 귀하의 케이스에 access_request 인스턴스가 있고 모델에 get_abosulte_url() 메소드가 하나있는 경우 전자 메일 템플릿의 다음 줄에 절대 URL이 제공됩니다.

{{ protocol }}://{{ domain }}{% access_request.get_absolute_url %} 

참조 - django.contrib.auth.formPasswordResetForm.

2

요청 개체에 액세스 할 수없는 경우 사이트 모델의 관리자 인 get_current() 메서드를 사용할 수 있습니다. 당신은 전달하여 이메일로 귀하의 웹 사이트에 절대 URL을 생성 할 수 있습니다

from django.contrib.sites.models import get_current_site 
current_site = get_current_site(request=None) 
domain = current_site.domain 
protocol = "http" 

:

from django.contrib.sites.models import Site 

@receiver(pre_save, sender=AccessRequest) 
def email_if_access_true(sender, instance, **kwargs): 
    current_site = Site.objects.get_current() 
    if current_site.domain == 'foo.com': 
     #do other stuff here 
    else: 
     pass 

당신은 당신이 당신의 pre_save 신호 수신기에서 설정

+0

wrt/SITE_ID = 1 : 동일한 프로젝트에서 둘 이상의 사이트를 제공하기 위해 사이트 프레임 워크를 실제로 사용하지 않는 한) –