337π
# Older Django <3.0 (also deprecated in 2.0):
from django.contrib.staticfiles.templatetags.staticfiles import static
# Django 3.0+
from django.templatetags.static import static
url = static('x.jpg')
url now contains '/static/x.jpg'
, assuming a static path of '/static/'
94π
EDIT: If youβre on Django >=3.0, refer to Django get the static files URL in view instead. This was answered with Django 1.X version.
dyveβs answer is good one, however, if youβre using "cached storage" on your django project and final url paths of the static files should get "hashed"(such as style.aaddd9d8d8d7.css from style.css), then you canβt get a precise url with django.templatetags.static.static()
. Instead, you must use template tag from django.contrib.staticfiles
to get hashed url.
Additionally, in case of using development server, this template tag method returns non-hashed url, so you can use this code regardless of that the host it is development or production! π
from django.contrib.staticfiles.templatetags.staticfiles import static
# 'css/style.css' file should exist in static path. otherwise, error will occur
url = static('css/style.css')
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Django override save for model only in some cases?
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
35π
From Django 3.0 you should use from django.templatetags.static import static
:
from django.templatetags.static import static
...
img_url = static('images/logo_80.png')
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
- [Django]-Django: "projects" vs "apps"
- [Django]-Add a custom button to a Django application's admin page
15π
hereβs another way! (tested on Django 1.6)
from django.contrib.staticfiles.storage import staticfiles_storage
staticfiles_storage.url(path)
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-Is there a list of Pytz Timezones?
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
11π
Use the default static
tag:
from django.templatetags.static import static
static('favicon.ico')
There is another tag in django.contrib.staticfiles.templatetags.staticfiles
(as in the accepted answer), but it is deprecated in Django 2.0+.
- [Django]-Django staticfiles not found on Heroku (with whitenoise)
- [Django]-Django test app error β Got an error creating the test database: permission denied to create database
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
8π
@dyveβs answer didnβt work for me in the development server. Instead I solved it with find
. Here is the function:
from django.conf import settings
from django.contrib.staticfiles.finders import find
from django.templatetags.static import static
def get_static(path):
if settings.DEBUG:
return find(path)
else:
return static(path)
- [Django]-Querying django migrations table
- [Django]-Filtering using viewsets in django rest framework
- [Django]-How to use permission_required decorators on django class-based views
7π
If you want to get absolute url(including protocol,host and port), you can use request.build_absolute_uri
function shown as below:
from django.contrib.staticfiles.storage import staticfiles_storage
self.request.build_absolute_uri(staticfiles_storage.url('my-static-image.png'))
# 'http://localhost:8000/static/my-static-image.png'
- [Django]-Adding a user to a group in django
- [Django]-AngularJS with Django β Conflicting template tags
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
0π
In short words you need to get
STATIC_URL
STATIC_ROOT
urlpatterns
staticfiles
templatetags
url parameters
All in the right place, to get this working. In addition, in real time deployment, circumstances vary, which it is very possible that the current setting you spent 3 hours worked out, works on your local machine but the server.
So I adopted the traditional way!!
app
βββ static
β βββ json
β βββ data.json
βββ views.py
views.py
import os
with open(os.path.abspath(os.getcwd()) + '/app/static/json/data.json', 'r') as f:
pass
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Error: could not determine PostgreSQL version from '10.3' β Django on Heroku
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions