[英]Reverse url not showing the full url in django
這是我的代碼
#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
類的路徑
我沒有更改任何內容,只是為其他視圖添加了相同的代碼。
我的新代碼:
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)
仍然出現此錯誤:
unbound method build_absolute_uri() must be called with HttpRequest instance as first argument (got str instance instead)
Django的Request具有一種方法 :
HttpRequest.build_absolute_uri(location)返回location的絕對URI形式。 如果未提供任何位置,則該位置將設置為request.get_full_path()。
如果該位置已經是絕對URI,則不會更改。 否則,將使用此請求中可用的服務器變量來構建絕對URI。
示例:“ http://example.com/music/bands/the_beatles/?print=true ”
傳遞您從reverse
獲取的url
更新:在其他方面,Django Rest Framework提供了一個反向版本,可以構建一個絕對URL-如果您以前使用過,它將解釋更改。 (您更改了什么?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.