2015-02-04 7 views
0

에 전체 URL을 표시되지 않는 것은 이것은 내가 메일로 보내려면이 사용하던역 URL이 장고

#Send mail to admin 
admin_subject = "New question added" 
admin_message = "A new question has been successfully added. Please review and approve it." 
url = reverse("admin:appname_question_change", args=(new_question.pk,)) 
admin_html_message = "<h3>%s</h3>\ 
           <p>%s</p>\ 
           <a href='%s'>%s</a>" % (admin_subject, admin_message, url, url) 

내 코드입니다. 내 메일에 전체 URL이 표시되기 전에

http://192.168.1.0:8000/admin/appname/answer/12 좋아하지만 지금은 내가 아무것도 변경하지 않은 /admin/appname/answer/12

같은 경로를주고는 다른 뷰에 대한 동일한 코드를 추가했다.

내 새로운 코드 :

unbound method build_absolute_uri() must be called with HttpRequest instance as first argument (got str instance instead) 
+1

'reverse' 절대 절대 URL을 생성하지 않습니다. 다른 이메일 템플릿이나 '역방향'기능을 사용하여 이전에 URL을 만드셨습니까? –

+0

예 '역방향'기능을 사용하고있었습니다. –

답변

4

장고의 Request has a method for that : 당신이 reverse

에서 얻을 url

HttpRequest.build_absolute_uri(location) Returns the absolute URI form of location. If no location is provided, the location will be set to request.get_full_path().

If the location is already an absolute URI, it will not be altered. Otherwise the absolute URI is built using the server variables available in this request.

Example: " http://example.com/music/bands/the_beatles/?print=true "

패스

from django.http import HttpRequest 
admin_subject = "New question added" 
admin_message = "A new question has been successfully added. Please review and approve it." 
location = reverse("admin:appname_question_change", args=(new_question.pk,)) 
url = HttpRequest.build_absolute_uri(location) 
admin_html_message = "<h3>%s</h3>\ 
         <p>%s</p>\ 
         <a href='%s'>%s</a>" % (admin_subject, admin_message, url, url) 
mail_admins(admin_subject, admin_message, fail_silently=False, connection=None, html_message=admin_html_message) 

아직도이 오류가

업데이트 : Django Rest Framework에서는 절대 URL을 작성할 수있는 역방향 버전을 제공합니다. 이전 버전을 사용하고 있다면 변경 사항을 설명 할 수 있습니다. (무엇을 변경 했습니까?)