{{ STATIC_URL }} with pyjade in django - django

I have some trouble adding my CSS in my template using {{ STATIC_URL }}. The tag does not work inside the link markup, but works anywhere else.
This:
{{ STATIC_URL }}
correctly translated into
/static/
, but
link(href="{{ STATIC_URL }}css/bla.css"
become
<link href="{{ STATIC_URL }}css/bla.css">
I use pyjade with Django. Anybody can help on this?
edit
I found the answer:
if you want to use static attributes, use '!=' instead of '='. Example:
link(href!="{{ STATIC_URL }}css/bla.css"

Finally I found an answer from here : https://github.com/SyrusAkbary/pyjade/issues/44
use link href=STATIC_URL + 'css/bla.css'
or link href='#{STATIC_URL}css/bla.css'
In addition to this situation,I met some problems when I use jade to write for loop in the template.I used django template to instead jade's for loop.

You should use the proper static tag
{% static "file_to_include" %}
and load the static lib in your template.
BTW: Since there are no updates to pyjade and its successor recently was deleted from pypi, I took the liberty to revive it: https://github.com/kakulukia/pypugjs
Version 5.0.1 has fixed the incompatibility with recent Django versions.

Related

How to set multiple STATIC_URL for the same static data?

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.

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/

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

Serve different Static files on devel and production in Django

I have a production and local DJANGO development environment. To push things to production we have a deployer which minifies and gzips all CSS and JS files.
To serve them on production I need to call them like
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
However on development I want the normal css file served (that way I don't have to re-minify and gzip each time I save) with:
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
Is there any way to achieve and automatize this behaviour by adding something to the deployer?, is there some other work-around (I could get rid of the .min extension if it's possible to add the .gz in a clean way?
I want to note the I know I could implement some html-parser which adds it on each deploy but I'm looking for a neat and django oriented solution.
I like the #Nursultan idea. To enforce this you could code a context processor like this:
# On yourapp.context_processor.py
from django.conf import settings
def debug_set(request):
return {'debug_set': settings.DEBUG}
# On your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
.
.
.
'yourapp.context_processors.debug_set',
)
# On your templates
{% if debug_set %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
{% else %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
{% endif %}
As usual, there's a Django package for that! There are two I use:
django-compressor: http://django-compressor.readthedocs.org/en/latest/
django-pipeline: https://django-pipeline.readthedocs.org/en/latest/
I started on django-pipeline but have moved to using compressor as of late. See the docs, I believe one will be what you're looking for. Good luck!
I have never met this problem, but I come up with these two solutions:
Use different settings.py for production and development. But it requires to have the same names for *.min.js and change minifier's configuration.
Or use a global variable and write everywhere
{% if development_stage %}
<link>
{% else %}
<link>
{% endif %}
Django - How to make a variable available to all templates?

Django and OpenShift static files can't be found

So I followed this tutorial:
https://github.com/rancavil/django-openshift-quickstart/wiki/Tutorial-How-create-an-application-with-Django-1.6-on-Openshift
and I tried to add a static image to the default home.html page and it just won't find it.
HTML:
{% load static %}
<html>
<head>
</head>
<body>
<img src="{% static 'Logo_Trans.png' %}">
<div class="left"></div>
<div class="right">
<p>is coming soon</p>
</div>
</body>
</html>
All other files are as listed in the repo given.
I've found the problem. The static files were serving properly when deployed but not when in debug mode. To fix this just add the STATICFILES_DIR variable in settings.py
Find:
if ON_OPENSHIFT:
DEBUG = False
else:
DEBUG = True
Add:
if ON_OPENSHIFT:
DEBUG = False
else:
DEBUG = True
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),
'/var/www/static/',)
Openshift is a very good service but definitely needs to update their docs/examples for django. Their sample program still runs django 1.4. Hope this helps anyone else that runs into this problem.
you can read the answer for this question Django cannot find static files. Need a second pair of eyes, I'm going crazy. Hope it will help you to understand static files. Static files in Django are always a bit painful.
Btw, as I explain in the last comment, I recommend you to create an app called common within a static folder inside it to place static content that is not application specific. Static application specific files should be placed inside the static folder within your app. This way, you can forget about defining the STATICFILES_DIRS variable and it will always work in DEBUG mode.
After that, ofc, define STATIC_ROOT and when you want to work in deploy mode, do the collectstatic command and it will also work.
After dealing hundreds of times with issues like this, I've found this is the best approach.