Generating static URLs with the static template tag
In Exercise 5.01 – serving a file from an app directory, you set up an image file to be served by Django. We saw that the URL of the image was http://127.0.0.1:8000/static/landing/logo.png
, which you could use inside an HTML template. For example, to display the image with an img
tag, you could use this code in your template:
<img src="http://127.0.0.1:8000/static/landing/logo.png">
Or, since Django is also serving the media and has the same host as the dynamic template response, you can simplify this by just including the path, as follows:
<img src="/static/landing/logo.png">
Both addresses (URLs and paths) have been hardcoded into the template – that is, we include the full path to the static file and make assumptions about where the file is being hosted. This works fine with the Django dev server or if you host your static files and Django website on the same domain. For...