我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
当前回答
<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,这段代码呈现
其他回答
在你看来,只需这样做:
base_url = "{0}://{1}{2}".format(request.scheme, request.get_host(), request.path)
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]
我使用这个代码:
request.build_absolute_uri('/')[:-1]
回应:
https://yourdomain.com
试试下面的代码:
{{ request.scheme }}://{{ request.META.HTTP_HOST }}
检查请求。META字典。我认为它有服务器名和服务器端口。