I have a Django project that was started with Django 1.2. Now I'm trying to run it under Django 1.4, in a development environment, using the built-in webserver. One thing I cannot get working is the static files for the admin interface. In my django server's window, I see:
Django version 1.4, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Jul/2012 11:38:04] "GET /admin/ HTTP/1.1" 200 6452
[18/Jul/2012 11:38:05] "GET /admin/admin/css/base.css HTTP/1.1" 404 4249
[18/Jul/2012 11:38:05] "GET /admin/admin/css/dashboard.css HTTP/1.1" 404 4264
I see that ADMIN_MEDIA_PREFIX has been deprecated, but I'm clearly missing the HOWTO that tells me what I have to do to get the admin pages working in development.
Have you tried running collectstatic to gather your static files?
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#collectstatic
I finally started another project with "django-admin startproject", and started picking through the settings file. I found that the new settings file had:
added 'django.contrib.staticfiles' to INSTALLED_APPS
added a few STATIC_ settings. I copied these four:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = ()
And now things appear to be working.
In django 1.4 you can use the static tag
{% load static %}
<a href="{% static 'img/foo.png' %}">
Related
I added flask-admin admin panel for my FastAPI project. For the first view, it works, but it has no static. I found 404 in terminal:
"GET /static/admin/bootstrap/bootstrap4/swatch/Cosmo/bootstrap.min.css?v=4.2.1 HTTP/1.1" 404 -
So I can add this file in virtual environment but as for me it is not the best option and I decided to serve static from my project:
app = Flask(__name__, static_folder='static', static_url_path='/static')
I added bootstrap.min.css and bootstrap.min.css.map to
static/admin/bootstrap/bootstrap4/swatch/Cosmo
But it does not work. What's wrong?
My app consists of django and react, which at the end is bundled to static (.js) file.
My goal is to setup a nginx for it, but for now I just want to run in uwsgi to see if it works.
Aaaaand it does not work at all. I belive there is a issue with loading this bundle file which webpack is compiling.
Connection to uwsgi works, server is up and running, but when connecting to 127.0.0.1:8080 I get (in Mozilla):
The resource from “http://127.0.0.1:8080/static/frontend/main.js”
was blocked due to MIME type (“text/html”) mismatch
(X-Content-Type-Options: nosniff).
# and another
GET http://127.0.0.1:8080/static/frontend/main.js
HTTP/1.1 404 Not Found
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 3277
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Vary: Origin
Similar topic I found: Difference between static STATIC_URL and STATIC_ROOT on Django
and this: Django + React The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)
But messing with python manage.py collectstatic resolved nothing.
I have this lines of code to manage static files:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join('/app/label_it/front/static/frontend/')
And this is how I start my uwsgi connection:
python manage.py collectstatic --no-input
uwsgi --http 0.0.0.0:8080 --master --enable-threads --module label_it.wsgi
Beside collectstatic I've tried messing with <script src"static_file_path"></script> this static path with:
<script src="../../static/frontend/main.js"></script>
# and
<script src="{% static "frontend/main.js" %}"></script>
Which result in nothing.
Any clues appreciated !
EDIT:
My file structure:
Ok I just did some testing with my own server and django project to confirm my suspicion and I've come to the following conclusion: This error will happen if the path to the static file doesn't exist. Meaning that uwsgi cannot find this main.js file with the settings you have.
My suggestion would be to do the following to ensure your static files are being served correctly in a way that django knows where to look.
By default django searches inside each installed app for a static folder to find static assets. Because your project is not setup that way you have to specify additional directories where static files live.
This is what STATICFILES_DIRS is for.
In your settings.py put the following
STATICFILES_DIRS = [
BASE_DIR / 'label_it/front/static'
]
This will make all files inside label_it/front/static discoverable to django.
Now this means that
<script src="{% static "frontend/main.js" %}"></script>
will (hopefully) work.
additionally, remember to include your static urls in your urls.py file if you haven't already.
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
]
# static content urls
urlpatterns += staticfiles_urlpatterns()
Sidenote: STATIC_ROOT is intended to be the location where all static files will be collected to when you run python manage.py collectstatic.
As such it should be an empty directory, preferably in the root of your django project.
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
The name of the folder doesn't really matter and I've used static_collected as an example.
If you run the command hopefully you'll see all the files in label_it/front/static/ copied there.
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.
Using django 1.5
I got static files configured like this:
STATIC_ROOT = '/home/<user>/Projects/<name>/static'
STATIC_URL = '/static/'
i just run manage.py collectstatic
directory listing:
static/css
static/css/bootstrap.css
static/css/addressbook.css
static/css/bootstrap-responsive.css
static/css/rewrite.css
static/css/login.css
when I type
localhost:8000/static/css/addressbook.css i got 404
but:
localhost:8000/static/css/bootstrap.css
gives me a proper css content
WTF? they are in the same folder and have same user/rights/groups
part from menage.py runserver output:
[24/Jul/2013 12:18:19] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:19:16] "GET /static/css/login.css HTTP/1.1" 200 533
[24/Jul/2013 12:20:12] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:32:51] "GET /static/css/bootstrap.css HTTP/1.1" 304 0
UPDATE:
It's serving files not from "project/static" but from static folder under application folder
I figure that out by deleting static forder under one app - files start to give 404. Same if I disable AppDirectoriesFinder.
But it still not consistent some applications don't serve files even from "static" under application folder.
My ideal situation will be: AppDirectoriesFinder commented out and all files served from myProject/static/
You can't just drop files into the /static folder of your project. Django doesn't "see" files in that directory unless they were collected from the apps via collectstatic. You will get a 404 trying to access files that you drop in there manually.
You need to put static files into the /static directory of an app (if it doesn't belong in a specific app, just create a new one, for example "main" or "website").
Alternatively you can set the STATICFILES_DIRS for Django to search additional directories when you call collectstatic.
I had the same problem. Here's my solution:
Add this to your settings.py
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
#"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I'm trying to deploy this application with nginx/gunicorn but I don't know why the admin media files are missing.
settings.py:
ADMIN_MEDIA_PREFIX = '/srv/www/antingprojects.com.ar/gobras/static/admin/'
I also tried:
ADMIN_MEDIA_PREFIX = '/static/admin/'
project folder:
/srv/www/antingprojects.com.ar/gobras/static/admin/css|js|img
urls.py:
(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve')
nginx access.log:
"GET /admin/ HTTP/1.1" 200 1556 "-"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/base.css HTTP/1.1" 404 1136 "http://antingprojects.com.ar/admin/"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/dashboard.css HTTP/1.1" 404 1141 "http://antingprojects.com.ar/admin/"
"GET /admin_media/img/admin/nav-bg.gif HTTP/1.1" 404 1114
You need to set up a symbolic link from the directory where the admin media files are to the static media directory you are using to serve your static files.
The admin media files should be at something like:
path/to/django/contrib/admin/media/
Once your symlink is set up you would set your ADMIN_MEDIA_PREFIX to the symbolic directory within the directory where you serve your static files, so something like this should work:
ADMIN_MEDIA_PREFIX = '/static/admin/'
Check to see where your admin is trying to load its media files from, and this should help you get started if you can't figure out which directory to use.
Here's a quick tutorial on symlinks:
http://ubuntuforums.org/showthread.php?t=255573
Your ADMIN_MEDIA_PREFIX is correct, you just need to remove /admin portion from your static view URL, because what you have now specified translates to a URI beginning with /static/admin/admin for accessing your admin resources through the static view.
Thank you for your help, the problem was in the nginx.conf, I wasn't locating /static folder.
location /static {
root /srv/www/antingprojects.com.ar/gobras;
}