Get the absolute static file URL in my Django template

I want to know how can I get the absolute URL of my static file directly in my template in Django ?

For now in my template :

<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">

return

<link rel="stylesheet" href="/static/css/bootstrap.min.css">

How can I get on dev:

<link rel="stylesheet" href="http://127.0.0.1:8000/static/css/bootstrap.min.css">

on production

<link rel="stylesheet" href="https://mycompany.com/static/css/bootstrap.min.css">

There are two options:

  • recommended: use the sites framework to render the appropriate domain
  • not recommended: store your current domain as a Django setting in the settings file you use depending on your environment
  • I usually go for (1), the only downside being that you have to update the current domain in the DB, but that usually happens just once per deployment.

    Then the appropriate domain will be displayed irrelevant of where code is running; you should always use the static tag in your template, rather than handling the display of the domain manually.

    链接地址: http://www.djcxy.com/p/78050.html

    上一篇: R string * .txt',即使top dir没有.txt文件

    下一篇: 在我的Django模板中获取绝对静态文件的URL