In my dev. environment there is a /static/ folder, which atm. stores some images for the web-site.
My INSTALLED_APPS variable in settings.py does contain the
django.contrib.staticfiles app and
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'),)
My urlpatterns variable in urls.py has this
urlpatterns+= static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
Finally, in my template.html file i'm iterating through my cardset objects and am trying to provide an image for each object like this
<img src=" {{STATIC_URL}}/images/{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
Now, the problem is:
a.) the {{STATIC_URL}} resolves as an empty string.
b.) i think i cannot use the static tag here because the variable {{cardset.image}} would not work well inside of the django tamplate {% ... %} with the static tag.
Could you please advice on what I should try doing here?
It seems to me you can still use the static tag. To build your string without using an image field, your link just needs tweaking slightly:
<img src="{% static "images/" %}{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
which should render in html correctly. Depending what your {{cardset.image}} looks like you may be able to get rid of the images/ part in the middle.
Remember you also need to include {% load staticfiles %} at the top of your template.
Since cardset.image is a string, your assumption b is false; tags work perfectly well with variables:
{% static cardset.image %}
(The reason {{ STATIC_URL }} wasn't working was presumably because you haven't passed it to the template; it's not present automatically, it's just a standard template variable.)
Related
An already developed front end is making calls to the same static data files using different URLs
GET /static/images/1.png
and also using sometimes this:
GET /SomeDirName/static/images/1.png
Setting my STATIC_URL to "/static/" serve only the requests similar to the first example.
Is it possible to redirect the GET from the second example? Or how can I handle it?
Please note that Im not asking how to set STATICFILES_DIRS.
Instead of implementing a redirection mechanism, I strongly advise to solve the problem "upstream". The reason it aims to fetch the files with somedirname/static/images/1.png is because you do not use a leading slash in your URL. So you wrote something like:
<img src="static/images/1.png">
instead of:
<img src="/static/images/1.png">
It is however strongly advisable to use the {% static … %} template tag [Django-doc] and thus implement this as:
{% load static %}
<img src="{% static 'images/1.png' %}">
This will automatically generate the correct URL. If you later change the STATIC_URL setting [Django-doc], it will prepend the other STATIC_URL to the path, and thus make the use of static files more flexible.
I am using digitalocean.com spaces to store static files for my Django app. I set it up successfully according to their tutorial (same settings as AWS). I now want to put a CDN in front of the static files. KeyCDN has a document describing how to do this but suggests using {{STATIC_URL}} in templates rather than the {% static %} templatetag.
Django admin uses the {% static %} templatetag not {{ STATIC_URL}}. In some cases there is no difference, however, if you define STATICFILES_STORAGE, as is required to store static files in digitalocean.com spaces, the templatetag {% static %} ignores whatever you explicitly declare in settings.py for STATIC_URL.
I have:
STATICFILES_STORAGE='storages.backends.s3boto3.S3Boto3Storage'
S3Boto3Storage sets the template tag {% static %} to point to https://ams3.digitalocean.com/bucket_name/path/to/static/ regardless of the setting of {{ STATIC_URL }}.
Manually setting STATIC_URL= in settings.py as KeyCDN suggests:
STATIC_URL = 'http://keycdndjango-1c6b.kxcdn.com/static/'
has no effect on what the templatetag {% static %} returns.
So i cannot figure out how to make KeyCDN work with this setup.
Any help is appreciated!
Well, I don't know if this was the case, but I'm using Digital Ocean CDN and setting AWS_S3_ADDRESSING_STYLE to 'virtual' made the change
from
https://ams3.digitalocean.com/bucket_name/path/to/static/
to
https://bucket_name.ams3.digitalocean.com/path/to/static/
Can I modify the static_url setting in settings.py to another value, such as "/assets", without causing any issues?
You can change that value to whatever you'd like, without any serious issues, provided you are correctly using the {% static %} template tag to resolve URL paths to static assets.
I'm using Django 1.4
My static files don't seem to be running or {{ STATIC_URL }} is wrong in the html file.
In the settings, I have the static files loaded:
STATICFILES_DIRS = (
'C:/Users/dtc/Documents/Eclipse/java_applet/applet',
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
In url.py I have:
urlpatterns += staticfiles_urlpatterns()
In the html file, I have:
<html>
<title>The Hello, World Applet</title>
<img src="{{ STATIC_URL }}tets.png" />
<applet code="{{ STATIC_URL }}HelloWorldApplet.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
</html>
Now, when I runserver, I can download the static files from localhost:8000/static/file which means it should be there right? But when I load the page, neither the applet or the image is showing up. All I'm trying to do as an endgoal is to run an applet on my dev server but I can't seem to figure out why {{ STATIC_URL }} isn't working(I even added a backslash after it in case that was the reason).
Look at the RequestContext class, this is probably what you're looking for.
See this answer on SO as well.
I'm making a simple Django project but I cannot get any images to display in my pages.
Django documentation at https://docs.djangoproject.com/en/1.3/howto/static-files/#basic-usage states
Basic usage Put your static files somewhere that staticfiles will find
them.
By default, this means within static/ subdirectories of apps in your
INSTALLED_APPS.
Your project will probably also have static assets that aren’t tied to
a particular app. The STATICFILES_DIRS setting is a tuple of
filesystem directories to check when loading static files. It’s a
search path that is by default empty. See the STATICFILES_DIRS docs
how to extend this list of additional paths.
Additionally, see the documentation for the STATICFILES_FINDERS
setting for details on how staticfiles finds your files.
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
For local development, if you are using runserver or adding
staticfiles_urlpatterns to your URLconf, you’re done with the setup –
your static files will automatically be served at the default (for
newly created projects) STATIC_URL of /static/.
You’ll probably need to refer to these files in your templates. The
easiest method is to use the included context processor which allows
template code like:
See Referring to
static files in templates for more details, including an alternate
method using a template tag.
So I did this in settings.py:
STATICFILES_DIRS = (
'/home/abc/django/project1/media/',
)
and enabled 'django.contrib.staticfiles',
In media, I have a folder img, which has various jpg files.
In my template, I have this as one of the lines:
<img src="{{STATIC_URL}}img/{{var}}.jpg">
When I'm passing var to this template via my view.
The HTML page seems to render this tag as "<img src="img/abc.jpg"> where var="abc".
But my browser refuses to display the image. What have I done wrong?
Did you see ths part in the documentation:
If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.
Do you also use RequestContext in your view to render the template?
Here is an alternative approach:
settings.py:
import os
PROJECT_DIR = os.path.dirname(__file__) + '/../'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'media/')
STATIC_URL = '/media/'
In your template:
{% load staticfiles %}
<img src="{% static img/foo.jpg %}" />
If you need to pass a variable, use the prefix method:
{% load staticfiles %}
<img src="{% get_static_prefix %}img/{{var}}.jpg" />