How to set multiple STATIC_URL for the same static data? - django

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.

Related

KeyCDN and DO Spaces: Django {% static %} vs {{ STATIC_URL}} when STATICFILES_STORAGE is set

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/

I can modify the static_url in settings.py?

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.

Serving static images on django application?

I am have been having troubles figuring out how to serve static images with my django application. However, I have figured out a sort of "hack" to get it working but it doesn't allow for much flexibility.
In the html files I have tried the following...
<img src="{{ STATIC_URL }}textchange/nextarrow.png" class="foobar" id="foobar">
Above: The pages will load when I use this but the images will not work.
<img src="{% static "textchange/nextarrow.png" %}" class="foobar" id="foobar">
Above: The pages will not load, I get a server error
<img src="thefullurltoS3bucket/nextarrow.png" class="foobar" id="foobar">
Above: The images will load fine
I have been trying all these different formats when going to production with Heroku and S3. The question I have is is the way I'm serving my static images correct? I use the last example in which I make the src the full url to the bucket at s3. Is that fine?
It should be noted that when I use the static tag serving css files works fine. Images is the only problem.
Your first and second solutions ( that you got server error and not loading ) is the correct way of using static files which is:
<img src="{% static "textchange/nextarrow.png" %}" class="foobar" id="foobar">
review your settings.py file and set STATIC_URL and STATIC_DIRS and also STATIC_ROOT, if you got server error maybe you set the STATIC_URL or STATIC_DIRS wrong. you can find plenty of posts about these settings.
Also check your image url in browser ( sth like 127.0.0.1:8000/static/files/etc.jpg )see if you can access the image.
Serving static files in Django can be a pain.
First, make sure you use the static template tag correctly in your template:
{% load static from staticfiles %}
<img src="{% static 'path/to/image.jpg' %}">
In your settings, have your STATIC_URL point to something like '/static/', your STATIC_ROOT point to the directory where you'll actually serve your files post-collectstatic, and your STATIC_DIRS to where you'll place your static files within your project (i.e. 'static' directory).
If you're serving from S3, you'll probably follow a tutorial like this one.

Link to a django static file doesnt work

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.)

django {% static "admin/" %}' producing 'admin' with missing trailing slash in production ONLY

Ok very weird issue here.
In the base.html file for the admin site they have this:
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% static "admin/" %}{% endfilter %}";</script>
The important part is this {% static "admin/" %}. Its only used for a handful of things in javascript, one of them being the calendar icon url for the date widget.
Locally this works just fine, and the url ends up being /static/admin/. However on production, it ends up as http://myaws.s3.address/admin with no trailing slash.
I can't figure out for the life of me how this might happen. This is Django 1.4. My STATIC_URL for aws ends with a trailing slash. This has got to be in the staticfiles app somewhere right? I'm not sure how to track this one down.
GAH!
Found the problem. Its a bug in django storages (s3 specifically):
http://code.larlet.fr/django-storages/issue/121/s3boto-admin-prefix-issue-with-django-14