Serve different Static files on devel and production in Django - 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?

Related

How to locate websocketbridge.js in Django using channels websocket?

I am trying to implement websockets using channels in Django project. I am getting 404 for webscoketbridge.js Below is html template.
{% load staticfiles %}
{% block title %}Delivery{% endblock %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="{% static 'channels/js/websocketbridge.js' %}" type="text/javascript"></script>
Also, I tried to have a look in the virtualenv/lib/python3.5/site-packages/channels path, there is no js folder or any file named websocketbridge.js
Has anyone solved this issue?
The javascript bridge was removed in v2.1.4. Here's the commit: https://github.com/django/channels/commit/2a9d764ad03927581aa2bfcadccc3e953949cb98#diff-b582cbb2f8294afa8bbe26c4c360a01d
This bit me, in my book that breaks semantic versioning.
As #tobyspark said, the javascript wrapper has been completely removed in the Django-channels 2. You can read more on how the js WebSocket wrapper was working in channels 1 here.
the simplest workaround to clear that error in your browser is to create a file called websocketbridge.js in the path shown in the error, "static/channels/js/", or you can specify any other path in your HTML src attribute matching the location of the static files and then add the code from here.
But you have to find a better implementation. You can use ReconnectingWebSocket. In the channels 2 release documentation, it is stated there might be other third-party packages for the binding but I don't know any other.

django: url routing to hardcoded static and html files

I have a legacy project with a bunch of html files and static files (css, images, js, videos ...). All links in those html files are relative and hardcoded:
"css/main.css" or "img/my_img.jpg" etc.
I need to run that project using django.
I cannot change all those links to kind of
{% load static %}
<link rel="stylesheet" href="{% static 'css/main.css' %}">
but all recipes I found in internet suggest using exactly that method.
How can I:
1) route hardcoded urls like "css/main.css"
2) route hardcoded urls like "another.html" ?
This can be done with much ease like this<!--lets' say image-->
<img src="{% static 'static/images/image1.png' %}">.Please make sure the file directories match with that in the code.
Please visit https://docs.djangoproject.com/en/1.10/howto/static-files for detailed info

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.

PyCharm3, how to get rid of "cannot resolve directory" messages?

I am using PyCharm3 for developing and I get "Cannot Resolve Directory" errors in places like these:
<link href="/static/css/styles.css" rel="stylesheet" type="text/css" />
However, if I do this:
<link href="/my_project/static/css/styles.css" rel="stylesheet" type="text/css" />
I do not get the error any more. So I guess it must be something in PyCharm's settings that I can fix. But what is that?
This is my settings.py
import os
PROJECT_DIR = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(PROJECT_DIR, "staticfiles/")
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static"),
)
I also tried to set static/ as "sources" folder.
PS: This is not a django error. It is a PyCharm error. My web application is working fine.
I know it has been asked a while ago. I faced the same issue with Intellij with Python Plugin similar to Pycharm.
Fix is as follows.
Mark the template folder as Template source.
Then you can add the template language as django
in language and
frameworks Ctrl+Shift+S
See the following details for reference
Use this
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
as per docs found here: https://docs.djangoproject.com/en/1.6/howto/static-files/
I was having the same problem. What I've done to fix this was:
I've enabled Django support on Pycharm = Settings -> Languages & Framework -> Django

{{ STATIC_URL }} with pyjade in 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.