2011-04-27 6 views
0

프로필 페이지가 다음과 같습니다 : http://i.stack.imgur.com/Rx4kg.png. 관리에서 원하는 모든 응용 프로그램에서 send_email 함수를 제어하는 ​​"메일로 통지"옵션을 원합니다. 예를 들어 나는 장고 메시지를 사용하고 있으며 메시지를 보낼 때 개인 메시지와 이메일을 보냅니다. 사용자가 메시지를 받으면 전자 메일을 원하는지 여부를 지정할 수 있도록하고 싶습니다.사용자 프로필 페이지에서 기능 제어 (예 : 메일 보내기)

메시지/utils.py는

def new_message_email(sender, instance, signal, 
     subject_prefix=_(u'New Message: %(subject)s'), 
     template_name="messages/new_message.html", 
     default_protocol=None, 
     *args, **kwargs): 
    """ 
    This function sends an email and is called via Django's signal framework. 
    Optional arguments: 
     ``template_name``: the template to use 
     ``subject_prefix``: prefix for the email subject. 
     ``default_protocol``: default protocol in site URL passed to template 
    """ 
    if default_protocol is None: 
     default_protocol = getattr(settings, 'DEFAULT_HTTP_PROTOCOL', 'http') 

    if 'created' in kwargs and kwargs['created']: 
     try: 
      current_domain = Site.objects.get_current().domain 
      subject = subject_prefix % {'subject': instance.subject} 
      message = render_to_string(template_name, { 
       'site_url': '%s://%s' % (default_protocol, current_domain), 
       'message': instance, 
      }) 
      if instance.recipient.email != "": 
       send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, 
        [instance.recipient.email,]) 
     except Exception, e: 
      #print e 
      pass #fail silently 

은 분명히 instance.recipient.email는받는 사용자의 이메일입니다. 내 질문은 다음과 같습니다. 내 new_message_email에서 사용자가 전자 메일을 원하는지 확인하는 데 사용할 수있는 옵션을 내 프로필 관리에서 만드는 방법은 무엇입니까? 내 생각에 나는 사용자를 위해 데이터베이스에 값을 저장하고 new_message_email 함수에서 그 값을 확인해야한다. 내가 어떻게하는지 분명하지 않다. 내 userprofile/views.py에 새 함수를 만들고 userprofile/forms.py에 클래스를 만드시겠습니까? 내 userprofile/overview.html 템플릿에서 템플릿을 변경합니까? 이것이 올바른 접근법이라면 어떤 특성과 생각이 많이 도움이 될 것입니다!

답변

1

날씨를 저장하는 좋은 방법이 있거나 사용자가이 이메일을 보내지 않았 으면 creating a user profile으로 시작하는 것이 좋습니다. 이는 settings.py에서 AUTH_PROFILE_MODULE 설정을 사용하여 완료됩니다.

데이터를 저장 한 후에는 instance.recipient (이 User 개체라고 가정)에서 데이터에 액세스 할 수 있어야합니다. 따라서 코드를 변경할 수 있습니다.

if instance.recipient.get_profile().wants_emails and instance.recipient.email != "": 
    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, 
     [instance.recipient.email,]) 

완료 및 완료.

+0

실제로 완료되었습니다! BooleanField()를 내 프로필에 추가하여 설명 된대로 정확하게 사용할 수있었습니다. 감사합니다! – leffe