我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
当前回答
如果你想使用reverse(),你可以这样做:request。build_absolute_uri(反向(view_name, args = (obj。pk,)))
其他回答
检查请求。META字典。我认为它有服务器名和服务器端口。
试试下面的代码:
{{ request.scheme }}://{{ request.META.HTTP_HOST }}
不是绝对的url,但我正在寻找只是得到主机。如果你想在view.py中获得host,你可以这样做
def my_view(request):
host = f"{ request.scheme }://{ request.META.get('HTTP_HOST') }"
<div class='col-12 col-md-6'>
<p class='lead'>Login</p>
{% include 'accounts/snippets/form.html' with form=login_form next_url=request.build_absolute_uri %}
</div>
例如,在这里,我说加载表单,并告诉表单,下一个URL是当前的URL,这段代码呈现
class WalletViewSet(mixins.ListModelMixin, GenericViewSet):
serializer_class = WalletSerializers
pagination_class = CustomPaginationInvestment
def get_queryset(self):
######################################################
print(self.request.build_absolute_uri())
#####################################################
wallet, created = Wallet.objects.get_or_create(owner=self.request.user)
return Wallet.objects.filter(id=wallet.id)
你得到这样的输出
http://localhost:8000/v1/wallet
HTTP GET /v1/wallet 200 [0.03, 127.0.0.1:41608]