python - Django 绝对网址
问题描述
所以我是 django 的新手 我一直在研究 PHP CodeIgniter 在其中将绝对 URL 放入 href 我使用了一个base_url
通过调用 URL helper 调用的函数
<?php echo base_url().'user/login';?>
它给出的输出类似于
http://localhost/projectname/user/login
我在 Django 中想要同样的东西,但没有得到任何我能理解的答案或答案。我不想使用相对 URL,我的预期结果应该是这样的
{% url 'login' %}
上面的代码应该返回
http://localhost/projectname/user/login
解决方案
您可以在 django 中通过 2 种方式获取绝对 url。
一种是使用
HttpRequest.build_absolute_uri()
HttpRequest 中的方法构建您的自定义模板标签
或者只是在您的 django 模板中使用以下行。
<a href="{{ request.get_host }}{% url your_url_name %}">Link</a>
有关详细信息,请参阅此链接: https ://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.HttpRequest.get_host
推荐阅读
- assembly - LDR 指令 CortexM7 上的 UNALIGNED 使用错误
- typescript - $ref 类型转换导致编译器需要分号来终止行
- arrays - 如何创建一个字典,键作为 NSObject 的子类,值作为数组?迅速
- ssl - 我们需要将根证书分发给客户端吗?
- sql - 使用 has_and_belongs_to_many 和上下文进行 ActiveRecord 排序
- javascript - 使用暂停和恢复功能自动化滑块
- c - 2个线程并行运行并等待相同的条件
- vb.net - 来自 DB 的 TileControl DevExpress
- parallax - translateZ 视差滚动不起作用
- libgdx - Libgdx freetypefont 不显示右边框颜色