I'm been using Django Compressor to manage my coffee/less files and its great for development, but I've had some issues to make it work for my production deployment.
My idea is to have apache to host the static files, possibly in another server. I'm setting COMPRESS_OFFLINE = True on the settings.py file.
Then I do the following
python manage.py compress - This populates the CACHE directory in my static directory, where all static files will be collected.
python manage.py collectstatic - This collects static files from all the apps on my project (some of which don't use compressor) into my static directory.
Copy the static directory somewhere to be hosted with apache. And setup apache to serve the files.
Modify the static_url variable in the settings.py file to point to the static server.
If I open any page, I get the following error on my server, this only seems to happen when I have DEBUG = False and COMPRESS_OFFLINE = True on my settings.py file:
TemplateSyntaxError: Caught OfflineGenerationError while rendering:
You have offline compression enabled but key
"777ba26736d046ab043dc151e7e9a060" is missing from offline manifest.
You may need to run "python manage.py compress".
When I check the static/CACHE directory, I confirm what the error says, this is my manifest.json file:
{
"6189b8598993d1cbdbd35d4dfd1a6711": "<script type=\"text/javascript\" src=\"http://192.168.1.123/CACHE/js/2f6ca6616bd6.js\"></script>",
"5c66dbed0e5b766c6e32773cd8585f3c": "<link rel=\"stylesheet\" href=\"http://192.168.1.123/CACHE/css/154d95903951.css\" type=\"text/css\" />"
}
If I delete the CACHE directory and rerun python manage.py compress, I get a new set of ID's both on the error message and the manifest file, but the ID on the error is still missing on the manifest.
So, I guess there are two questions here. Why is it not working? What is the proper way to achieve this?
Thanks.
If you've run compress, and you still get the message
OfflineGenerationError: You have offline compression enabled but key "4971a40e3b459a8cda8287a7f7caa96d" is missing from offline manifest. You may need to run "python manage.py compress"
then it's likely you have dynamic content inside compress tags. Make sure that compress is always the innermost block, and that there are no tags inside the compress block.
I guess you're using django-compressor 1.1.2 which doesn't support static template tag {% static "..." %}.
Try installing the dev version of django-compressor with:
pip install django_compressor==dev
It should solve the problem.
David Wolfe is absolutely right: had to dig throught all the code of mine to get rid of {% trans... etc.
I make it like this:
<script>
window.__enter_email = "{% trans "Enter correct email" %}"
window.__url = "{% url "shop:go" %}"
</script>
{% compress js %}
<script>
$("#bla")..... window.__enter_email ...
</script>
{% endcompress %}
Hope, helps someone!
Related
I am getting errors with a Django app on Heroku when referencing static files (e.g. CSS).
The files are found when running the site on my local Linux machine, but not when it is deployed on Heroku.
I have looked at the Django documentationrelating to static files, but I do not understand what I am doing wrong.
django.contrib.staticfiles is included in my INSTALLED_APPS.
In my_site/settings.py I have STATIC_URL = '/static/'.
In my HTML template I have
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'reco/style.css' %}">
The result is that the CSS file is found when running locally, but not on Heroku.
How can I fix this? I want the simplest possible solution - this is a very small site.
# Log message when running the app on the local machine: file is found and has not changed (correct)
[13/May/2020 15:02:52] "GET /static/reco/style.css HTTP/1.1" 304 0
# Log message when running the app on Heroku: not found. Why?
2020-05-13T20:09:20.327218+00:00 app[web.1]: Not Found: /static/reco/style.css
Update: Following suggestions from comments, I have configured the site as advised in the Heroku Django documentation
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'reco', 'static'),
)
Now when I deploy to Heroku I see that collectstatic is collecting my static files - but Heroku is stil not finding them at runtime.
As per the Dev Center of Heroku...
You need to install whitenoise to be able to serve static files on production mode.
Follow the installation instructions over here.
I just started a new project and currently Django can't find the static files. I'm using Django==2.2.6
The static files are located in an app called "website". This is the file structure.
https://i.imgur.com/AnPACop.png
This is from the settings:
STATIC_URL = '/static/'
This is how i include the static file:
{% static 'css/style.css' %}
The URL to the static file seems correct:
<link href="/static/css/style.css" rel="stylesheet">
EDIT: its NOT correct. But this works:
<link href="/static/core/css/style.css" rel="stylesheet">
Make your file structure like the following one:
ProjectFolderName
static
- css
- js
template
website
projectfoldername
migrations
Put your static folder in your project folder. Then make these changes to your settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
Then run this command:
python manage.py collectstatic
You static file will be copied to New file created by django as assets.
and add to your HTML
{% load static %}
This is the URL that the browser will find your static files. It won't let Django know in which folder to find them inside your project root (`BASE_DIR)
STATIC_URL = '/static/'
Try using this instead to specify the directory you are storing the statics
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'website/static'),)
Also, make sure you are loading the statics in your template with the following template tag
{% load static %}
Update
The path to the CSS is also wrong on the html you should change it to:
<link href="/static/core/css/style.css" rel="stylesheet">
It's solved. The problem was the file structure. For some reason the static files was in a core-folder.
https://i.imgur.com/AnPACop.png
When i put the files directly in "static" it started working.
My english is not perfect, sorry in advance.
I'm in Django 3 and I had the same problem. This how I find what's wrong, with the help of everyone up me. Just consider this post like a note for me.
I do :
python3 manage.py runserver
At this moment I read the last line of the output. It was looking in a file that did'nt exist. I copied the path. Go in terminal and :
cd path/copied/before/static/base.css
File not found. At this moment I know what to do. Just follow the path and create the folder I need.
I know it's not a good practise but it's can help beginner.
I want to translate a part of the JS in Django.
I've try the command python manage.py makemessages -d djangojs but it take only file in TEMPLATE_DIRS in the settings.py
I've try to set a JS in a template directory, and it work perfectly.
I've the djangojs.po and i can generate the .mo when i compile.
So the question is : How make message in static file?
I've found the same problem
Here
and
Here but no one answer who keep a good architecture.
Please, save me!
My architecture:
myapp
locale
static
myapp
js
try.js
template
myapp
try.html
views.py
urls.py
[...]
PS: Sorry for my english, i'm not native ;)
My error is when i set the STATIC_ROOT in settings.py. In fact this variable say at Django where stock Static when it do a CollectStatic on the server, i used her for say where found my static (Django can find all static whitout informations, it find static on a folder static on the project folder or on the app folder)
Finally :
set this in the urls.py of the pro
js_info_dict = {
'domain': 'djangojs',
'packages': ('app.kanboard',),
}
urlpatterns = patterns('',
[...]
#Internationalization Javascript
url(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
)
In the template
<script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>
In the JS in static/my_app/my_file.js
document.write(gettext('Ma chaine de caractère a traduire'))
After, we rule this command-line
python manage.py makemessages -d djangojs
Here, djangojs is the domain set in urls.py at the begin (It's a pseudo-convention)
At this time, we have a djangojs.po in the folder locale what we can compile as a standard .po.
Link of the doc : Here
Lik of my ticket where you can find a sample project and explication in english : The ticket
Good luck !
I am attempting to deploy a Django 1.6 application on OpenShift using the Python 3.3 cartridge, but I have run into problems with static files. I have had partial success with the OpenShift IRC channel, tutorials/templates (for example), and previous StackExchange questions (for example), but nothing has completely resolved the problem.
When I request the static content by URL (e.g. 'mydomain.com/static/stylesheet.css' or 'mydomain.com/static/icons/cog.svg') I can see them perfectly fine. When static files are used as SVG data for icons, they show up fine. Only when linking to a stylesheet have I run into problems. I use the following to include CSS in my template:
<link type="text/css" rel="stylesheet" href={% static "stylesheet.css" %}/>
I have loaded the static files tag set with {% load staticfiles %}. Instead of seeing the stylesheet at /static/stylesheet.css, Django (I assume that it is Django, not Apache) looks for it at /static/stylesheet.css/ (note the trailing slash). This causes the request to fail with a 404 status code. The same thing occurs when I use other file extensions (I have tried .txt, .css, and .svg) or link to a file contained in a subdirectory of static. It is only in this circumstance that an extra trailing slash is appended.
It is my understanding that Django appends a trailing slash to a URL in the event that the URL does not match any of the patterns defined in urls.py. Is it possible on OpenShift to configure Apache so that it directly handles all requests to URLs of the form /static/*? I have an .htaccess file in the wsgi directory with the commands
Rewrite Engine On
Rewrite Rule ^application/static/(.+)$ /static/$1 [L]
but this does not solve the problem. I have also tried using a rewrite rule for just the stylesheet as well as a few things with Alias but have had no luck there, either.
Should Django be getting the requests for these static files at all? I have confirmed that DEBUG is being set to False in my settings.py file, and make no mention of django.views.static.serve in my urls.py file. Here are the relevant parts of settings.py:
STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'),
'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
I do not set values for STATICFILES_DIRS or STATICFILES_FINDERS because at present I'm only dealing with static files found at STATIC_ROOT. The OpenShift project looks like
~/app-root/runtime/repo/wsgi/
.htaccess
application
openshift/
settings.py
manage.py
#And so on.
static/
stylesheet.css
icons/
cog.svg
#More icons here.
This is my first time trying to deploy and I am stuck on this stumbling block. Does anyone know what I am doing wrong?
Instead of href={% static "stylesheet.css" %}, try href="{% static 'stylesheet.css' %}"
PyCharm (1.3 and 2 beta) in my Django 1.3 project throws a lot of "unresolved static reference" errors when inspecting my templates for script and style includes.
In an outdated PyCharm doc, I found that a small guide that doesn't work in my situation, because my static files are spread over multiple apps. Adding my static dirs to STATICFILES_DIRS also didn't work.
Dir structure (simplified):
app1/static/js/file.js
app1/static/css/file.css
app2/static/js/otherfile.js
app2/static/css/otherfile.css
templates/template.html
Template.html:
<script src="{{ STATIC_URL }}js/file.js"></script>
file.js resolves when I visit the template on localhost, but not in PyCharm.
How do I make static files resolve in PyCharm?
Go to Settings in Pycharm 2.73
Settings >> Project Setting >> Django
Enable the Django support and provide the paths for the three following files:
Project Root
Settings file
Manage.py file
When you have given these informations, close PyCharm and restart it.
PyCharm 2.5 finds my static files again.
The trick is to mark app1/static and app2/static as "Source Root".
STATICFILES_DIRS is not working for me.
The selected answer doesn't work for me. What solved it is using a prefix in STATICFILES_DIRS:
STATICFILES_DIRS = (
# ...
("resources", "C:/data/django/myproject/myapp/static"), )
as documented in the django docs: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/
Then in your html template:
<link rel="stylesheet" href="{%static 'resources/favicon.png' %}" type="text/css">
STATICFILES_DIRS works for
{% static "js/lib/jquery-1.8.2.min.js" %}
tag in template.
Not for {{ STATIC_URL }}js/lib/jquery-1.8.2.min.js
http://youtrack.jetbrains.com/issue/PY-5568