gpt4 book ai didi

python - 在 Django 中使用 HttpRequest.build_absolute_uri(location) 时出现错误

转载 作者:行者123 更新时间:2023-12-01 04:51:26 27 4
gpt4 key购买 nike

我的代码:

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。

最佳答案

我认为您收到的错误非常明确。 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)

关于python - 在 Django 中使用 HttpRequest.build_absolute_uri(location) 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com