I am having a problem with deployment of Django application using AWS Lightsail referring to this video.
The static file is not being loaded.
The static url and directory in my settings.py are as mentioned below:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
The static folder is in the base directory named as /static/.
and the httpd-app.conf file in the conf folder has:
Alias /static/ "/opt/bitnami/apps/django/django_projects/Project/static"
<Directory opt/bitnami/apps/django/django_projects/Project/static>
Require all granted</Directory>
Alias /media/ "/opt/bitnami/apps/django/django_projects/Project/media"
<Directory opt/bitnami/apps/django/django_projects/Project/media>
Require all granted
</Directory>
I have run python3 manage.py collectstatic command and the static files are loaded in static directory but the site does not load it.
Related
I have a Django live version in production and I develop in local with Django manager.py.
I'm not able to find the correct configuration settings for my static files.
All my static files are on a /static/ on the root of the project, because all the sub-apps use the same stylesheets and so on.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
On my live version : it works because I've got an NGINX rule that allow access to /static/ directly.
But when I want to work on local, the static files are 404, with Debug True and False.
I don't see the interest for my needings of the staticfiles directory (but I can have a wrong mind here).
Is it possible to have all the static files in subdirectories in /static/ (css, img, js, etc.) and to set a workable settings on both local and live production without switching something in settings.py for deployment ?
Thanks for your help :)
I've found the solution :
After moving my static directory from the root directory of Django the folder of my main app, I made a collectstatic with that settings :
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
My site works well in local but when I put it on my server, it can't load the static files. Here's my files.
settings.py
DEBUG=False
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
All my static are in /myproject/mymainapp/static
I've got another directory "static" of the root of my project by doing manage collectstatic in production.
Here's my nginx conf file :
[...]
root /home/user/site/mymainapp/;
[...]
location /static {
alias /home/user/site/mymainapp/static;
}
I've tried to set the path to my static directory in my root directory project but it don't make anything better.
My URLs patterns :
urlpatterns = [
[...] nothing relative to static files
]
Maybe I should add something in my URLs patterns ?
Thanks for your help :)
To serve the static files (CSS, JS), I set the settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
This means that all my static files need to be in the project root's static folder
/app1
/app2
/media
/static #They need to be stored here, all static files
/templates
urls.py
settings.py
manage.py
However, I am storing them in my app's static folder.
/app1/static/ # Storing the static files here
/app2/static/ #and here
/media/
/static/ # but not here
Still, Django is able to serve them, how is that possible?
I tried the same thing with media files; setting
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and storing the media (images etc) files in each app's individual media dir. This time, Django did not serve the files and served them only when either I moved the files to the project root's media dir or changed the setting to MEDIA_ROOT = os.path.join(BASE_DIR, '<app_name>/media')
Why was I allowed to serve static files even when they weren't in the project root static dir but not the media files - they were only served from the root media dir.
Please read the documentation carefully - https://docs.djangoproject.com/en/1.9/ref/settings/#staticfiles-finders
It precisely states that django will search for static files in each app + the directory stated in settings.
Quoting as in the documentation -
The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.
While this is not true for media files. Django doesn't look for media files in subdirectories.
I've been a django developer for years now, yet a small practice project with 1.7 is giving me a great headache when it comes to serving static files.
I've set STATIC_ROOT
STATIC_ROOT = '/var/www/mydomain/static'
I've set STATIC_URL
STATIC_URL = '/static/' #as default
I'm not using STATICFILES_DIRS since I have one app called 'pages' and it's in INSTALLED_APPS. On localhost the static files are served correctly
I'm using Ubuntu 14.4 and Apache/2.4.7
My apache conf is
<VirtualHost *:80>
ServerName mydomain.net #I own the domain and pointed it correctly in GoDaddy
ServerAlias www.mydomain.net
Alias /static/ /var/www/mydomain/static
<Directory /var/www/mydomain/static>
Require all granted
</Directory>
WSGIScriptAlias / /var/www/mydomain/mydomain/wsgi.py
<Directory /var/www/mydomain/mydomain>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mydomain.net python-path=/var/www/mydomain:/var/www/mydomain/venv/lib/python2.7/site-packages
WSGIProcessGroup mydomain.net
</VirtualHost>
I've run collectstatic and confirmed all static files are in /var/www/mydomain/static/*.
The site loads, but I get a 404 on all css and js files.
All debugging efforts have failed. I've removed the STATIC_ROOT dir to expect a 403, but still getting a 404. I've chown'd all files/folder to root for testing; nothing. I created a deploy user and chown'd all files/folders to it; nothing. I've chown'd all files/folders to www-data; nothing!!
Is there a new config in Apache 2.4+ that's throwing me off?
What you can do is check your /var/www/mydomain path and look for the folder. I think its not there yet. If not then copy your folder to this place so that apache can serve it.
That will be worth looking
I have installed a django application with django-tinymce and django-filebrowser. In my machine every thing works fine. when I deployed the application to the production, filebrowser fails to upload file and it shows me the error message Failed
Here is my settings
MEDIA_ROOT = '/srv/www/staticFiles/APCC/'
MEDIA_URL = '/static/APCC/'
STATIC_ROOT = '/srv/www/staticFiles'
STATIC_URL = '/static/'
and here is my apache configs
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE APCC.settings
PythonDebug Off
PythonOption django.root /srv/APCC
PythonPath "['/srv/APCC/'] + sys.path"
</Location>
Alias "/static/" "/srv/www/staticFiles/"
<Location "/static">
SetHandler None
Order deny,allow
Allow from all
</Location>
</VirtualHost>
I have make sure that the MEDIA_ROOT directory is created and the uploads folder required by the filebrowser is also created under it.
Also the MEDIA_ROOT directory and the uploads directories have write permissions.
I believe the problem is in my apache configuration or file structure. where would be the problem? also is there any logs specific for tinycme or filebrowser to check?