2017-11-08 2 views
2

내 코드가 작동하지 않는 이유를 이해할 수 없습니다. 작동하기 전에,하지만 지금은 서버를 실행하고 테스트 할 때 코드가 작동하지 않습니다. 키워드 인수가 uidb64이고 장고 2.0이있는 NoReverseMatch

사용자가 등록하는

, 나는 다음과 같이 그에게 활성화 이메일을 보내 :

def send_activation_email(serializer, request, user): 
    current_site = get_current_site(request) 
    message = render_to_string('acc_active_email.html', { 
     'user': user, 
     'domain': current_site.domain, 
     'uid': urlsafe_base64_encode(force_bytes(user.pk)), 
     'token': account_activation_token.make_token(user), 
    }) 
    mail_subject = 'Activate your blog account.' 
    to_email = serializer.data['email'] 

    email = EmailMessage(mail_subject, message, to=[to_email]) 
    email.send() 

acc_active_email.html

{% autoescape off %} 
Hi {{ user.username }}, 
Please click on the link to confirm your registration, 

http://{{ domain }}{% url 'activate' uidb64=uid token=token %} 
{% endautoescape %} 

내 URL 파일

. 
. 
    url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
     views.activate_account, name='activate'), 
. 
. 

하지만, 이 오류가 있습니다 :

Exception Type:  NoReverseMatch 
Exception Value:  

Reverse for 'activate' with keyword arguments '{'uidb64': b'NDM', 'token': '4qz-8f770502bd8b02786da9'}' not found. 1 pattern(s) tried: ['activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$'] 

하이라이트이 줄 http://{{ domain }}{% url 'activate' uidb64=uid token=token %}

+0

uid가 바이트이기 때문에 작동하지 않는지 궁금합니다. ''uid ':'NDM '을 하드 코딩하여 오류를 수정했는지 확인하십시오. – Alasdair

+0

@Alasdair 와우, 'uid'를 설정하면 : 'send_activation_email' 메소드에서'NDM '이 작동합니다. 이상하게도 – r2546971

+0

URL을 되돌릴 수 있으려면 바이트를 문자열로 변환해야하는 것처럼 보입니다. – Alasdair

답변

4

장고 2.0에서 당신이 UID를 암호화하는 문자열로 변환 base64로 후 decode() 호출해야합니다 :

message = render_to_string('acc_active_email.html', { 
    'user': user, 
    'domain': current_site.domain, 
    'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(), 
    'token': account_activation_token.make_token(user), 
}) 

이 더 많은 정보를 위해 Django 2.0 release notes에있는 참고를 참조하십시오를 .

+0

감사합니다. – r2546971

+0

감사합니다. 나를 위해 일해. –

관련 문제