2011-11-01 28 views

Trả lời

6

Đây là cách bạn có thể thực hiện ghi đè:

urls.py

url(r'^user/password/reset/$', 
    'YOUR_APP.views.password_reset', 
    {'post_reset_redirect' : '/#/login?resetemail=true'}, 
    name="password_reset"), 

views.py

from django.contrib.auth.views import password_reset as django_password_reset 
from YOUR_APP.forms import CustomPasswordResetForm 

def password_reset(*args, **kwargs): 
    """ 
     Overriding the Email Password Resert Forms Save to be able to send HTML email 
    """ 
    kwargs['password_reset_form'] = CustomPasswordResetForm 
    return django_password_reset(*args, **kwargs) 

form.py

from django.contrib.auth.forms import PasswordResetForm 
from django.contrib.auth.tokens import default_token_generator 

class CustomPasswordResetForm(PasswordResetForm): 
    """ 
     Overriding the Email Password Resert Forms Save to be able to send HTML email 
    """ 
    def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', 
      use_https=False, token_generator=default_token_generator, request=None, email_subject_name='registration/password_reset_subject.txt', **kwargs): 
     from django.core.mail import EmailMultiAlternatives 
     from django.utils.html import strip_tags 
     from django.template.loader import render_to_string 
     from django.contrib.sites.models import get_current_site 
     from django.utils.http import int_to_base36 

     for user in self.users_cache: 
      if not domain_override: 
       current_site = get_current_site(request) 
       site_name = current_site.name 
       domain = current_site.domain 
      else: 
       site_name = domain = domain_override 

      c = { 
       'email': user.email, 
       'domain': domain, 
       'site_name': site_name, 
       'uid': int_to_base36(user.id), 
       'user': user, 
       'token': token_generator.make_token(user), 
       'protocol': use_https and 'https' or 'http', 
      } 
      render = render_to_string(email_template_name, c) 
      render_subject = render_to_string(email_subject_name, c) 

      msg = EmailMultiAlternatives(render_subject, strip_tags(render), None, [user.email]) 
      msg.attach_alternative(render, "text/html") 
      msg.send() 
+2

tôi nhận được lỗi này: AttributeError ở/người dùng/mật khẩu/reset/ 'CustomPasswordResetForm' đối tượng không có att sườn 'users_cache'. Bạn có nhầm lẫn không? Tôi có nên sử dụng một số chức năng khác thay vì users_cache()? Những người khác làm gì. –

5

Bạn có thể ghi đè phương thức save của django.contrib.auth.forms.PasswordResetForm và chuyển biểu mẫu mới làm đối số cho chế độ xem password_reset.

0

Sau khi một số lượng thử và sai, tôi khám phá d một cách nhiều hơn, nhiều hơn nữa terse để cung cấp một email đặt lại mật khẩu tùy chỉnh templated trong phiên bản mới nhất của Django (1.8).

Trong project/urls.py của bạn, thêm những hàng nhập khẩu:

from django.contrib.auth import views as auth_views 
from django.core.urlresolvers import reverse_lazy 

Và thêm các tuyến đường sau trong urlpatterns của bạn trước khi thông thường django contrib auth url tuyến đường bao gồm:

url(r'^accounts/password/reset/$', 
    auth_views.password_reset, 
    { 
    'post_reset_redirect': reverse_lazy('auth_password_reset_done'), 
    'html_email_template_name': 'registration/password_reset_html_email.html' 
    }, 
    name='auth_password_reset'), 


url('^', include('django.contrib.auth.urls')), 

Và sau đó, trong ứng dụng của bạn templates/registration thư mục, tạo password_reset_html_email.html với bất kỳ mẫu HTML nào bạn muốn.

Lý do này dường như lay cần thiết trong nguồn cho django/contrib/auth/views.py, trong đó có chức năng xem các tuyến đường URL ban đầu được ánh xạ tới:

147 def password_reset(request, is_admin_site=False, 
148     template_name='registration/password_reset_form.html', 
149     email_template_name='registration/password_reset_email.html', 
150     subject_template_name='registration/password_reset_subject.txt', 
151     password_reset_form=PasswordResetForm, 
152     token_generator=default_token_generator, 
153     post_reset_redirect=None, 
154     from_email=None, 
155     current_app=None, 
156     extra_context=None, 
157     html_email_template_name=None): 
158 

Các html_email_template_name được thiết lập để None như mặc định, và có không dường như là một cách để gán giá trị của nó, ngoài việc viết lại tuyến đường cụ thể này cho trường hợp này như tôi đã đề cập ở trên.

Hy vọng rằng điều này sẽ giúp mà không cần phải sao chép-dán một loạt các mã gần như giống hệt nhau như một số câu trả lời khác được đề xuất - phản hồi được chào đón, tất nhiên!

0

Đối với tôi, nghiên cứu tốn nhiều thời gian nhưng giải pháp khá nhỏ nhặt. Không có phần ghi đè nào không có các biểu mẫu hoặc bất kỳ thứ gì như thế đang sử dụng Django == 1.8.6 nhưng phải hoạt động từ ít nhất django 1.7 trở đi. Để kích hoạt tính năng hỗ trợ cho các email định dạng html ở password_reset tất cả tôi phải làm là thay đổi mẫu email tên quan trọng trong chức năng thiết lập lại từ email_template_name = 'email/password_reset_email_html.html

để

html _email_template_name =' email/password_reset_email_html.html',

do đó các chức năng thiết lập lại sẽ trông như thế:

def reset(request): 
# Wrap the built-in password reset view and pass it the arguments 
# like the template name, email template name, subject template name 
# and the url to redirect after the password reset is initiated. 
return password_reset(request, template_name='profile/reset.html', 
    html_email_template_name='emails/password_reset_email_html.html', 
    subject_template_name='emails/reset_subject.txt', 
    post_reset_redirect=reverse('success')) 
0

Dựa trên giải pháp Cem Kozinoglu Tôi sẽ đề nghị thay đổi hình thức và tình trạng quá tải send_mail thay vì phương pháp tiết kiệm theo cách sau:

class CustomPasswordResetForm(PasswordResetForm): 

    def send_mail(self, subject_template_name, email_template_name, 
        context, from_email, to_email, html_email_template_name=None): 
     """ 
      Sends a django.core.mail.EmailMultiAlternatives to `to_email`. 
     """ 
     subject = loader.render_to_string(subject_template_name, context) 
     # Email subject *must not* contain newlines 
     subject = ''.join(subject.splitlines()) 
     body = loader.render_to_string(email_template_name, context) 

     email_message = EmailMultiAlternatives(subject, body, from_email, [to_email]) 
     # New line introduce 
     email_message.attach_alternative(body, 'text/html') 

     if html_email_template_name is not None: 
      html_email = loader.render_to_string(html_email_template_name, context) 
      email_message.attach_alternative(html_email, 'text/html') 

     email_message.send() 
0

bạn có thể sử dụng PasswordResetSerializer http://django-rest-auth.readthedocs.io/en/latest/configuration.html

Sau đó, bạn có thể ghi đè lên tất cả các tùy chọn hình thức:

domain_override subject_template_name email_template_name use_https token_generator FROM_EMAIL yêu cầu html_email_template_name extra_email_context

trong trường hợp của tôi tôi chỉ cần ghi đè lên 2 đạo cụ

class CustomPasswordResetSerializer(PasswordResetSerializer): 

    def get_email_options(self): 
     return { 
      'domain_override': 'anydomain.com', 
      'html_email_template_name': 'your_temp/password_reset_email.html', 
     } 
Các vấn đề liên quan