在Django中使用HttpRequest.build_absolute_uri(位置)时出错

时间:2015-02-07 05:19:59

标签: python django

我的代码:

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)

我已经尝试了很多,但不知道导致此错误的原因。 我希望通过电子邮件发送绝对的uri。

1 个答案:

答案 0 :(得分:2)

我认为你得到的错误非常明确。 build_absolute_uri是一个实例方法,因此应由HttpRequest实例调用。

>>> from django.http import HttpRequest

>>> request = HttpRequest()
>>> location = reverse("admin:appname_question_change", args=(new_question.pk,))
>>> url = request.build_absolute_uri(location)