Django points to different locations for static files - django

I have deployed django website on debian 8 vps. The static files are held in static folder witch is at the same level as the app folder. The server can reach them on debian VPS. However the development version is located on Windows10 machine. The files are identical (cloned git repositories) but in the developement version tI get 404 on static/css/ when I try to reach my static files.
From my setting.py file:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT=os.path.join(BASE_DIR, 'static')
UPDATE:
I have moved my files to project_name/app_name/static (just as it is recommended in django documentation ) in both versions of the project. The effect is that now development server sees the files and deployed version does not (i.e. it displays raw html without any css styles) - so exactly the opposite to the previous situation.
I am out of ideas.
(maybe it has something to do with the fact that at certain point I did collectstatic so I have static folder in two locations???)

In Django you must coexist with 2 settings.
When DEBUG = True, the enviroment serves staticfiles itself if the settings variable STATICFILES_DIRS where pointed to path.
For me:
STATICFILES_DIRS= [os.path.join(PROJECT_ROOT, 'static').replace('\\','/'),]
When DEBUG = False the responsable for handling the staticfiles is Nginix/Apache.
In the project you shoud point the URL:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
If you have a static folder in every app you can use:
python manage.py collectstatic
This grabs all your static files and put them on the same static folder (STATIC_ROOT)
Then your Ngnix/Apache also must to know where statifiles are stored
(An example using Ngnix):
server {
access_log /pathto/log/acces.log;
error_log /pathto/log/error.log;
server_name ******
charset utf-8;
location /static {
alias /path/to/your/static; <---- This Line
}
location / {
uwsgi_pass django;
include uwsgi_params;
uwsgi_read_timeout 600;
}
}
Always is good to take a look at documentation

Related

django Static files in production server

My problem is with a static files (img , styles). My code is working fine with no error with static files and the images appears when I run the server, but when I make the (debug = False) the images and styles disappear I ran my code on Linux server ubuntu.... How do I make the static files appear with the (debug=false)?
in settings.py that is my code
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'music/templates/'),
)
In my html file the image-tag is just like this:
<img src="{% static 'music/login.jpg' %}" width="300" height="300">
As Jon Clements commented, if debug=False then the Django app staticfiles is disabled. Usually in production one uses a dedicated web server (a server that is not running Django to serve static files). Hence, to solve your problem, an extra server should be set up to serve the static files.
Some high level steps:
Go to your django settings file and add a STATIC_ROOT which will be the directory where Django will place the static files;
Run python manage.py collecstatic for Django to collect all the static files and put them at the STATIC_ROOT directory;
Serve the files in this directory when a request contains your STATIC_URL in the URL (these requests will usually contain /static/ or something). For example, this can be done with an Nginx server or an Apache one as explained in Django docs:
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/modwsgi/#serving-files
In terms of an Nginx config, one short snippet, just to illustrate such idea, could be something like:
server {
listen 80;
# hey Nginx, once you see something like /static/ in the url
# try to grab the file that is under the alias directory and
# return to the user
location /static/myapp/ {
alias /var/www/myproject/static/myproject/myapp/;
access_log true;
add_header Cache-Control "max-age=120";
}
}
To sum things up:
Django's staticfiles app is great for local development but is not really suitable for production apps.
Hence, serving static files should usually be done by a dedicated and separated server such as Nginx or an Apache one which requires some additional configuration to set these servers up.

Using static folding inside app folding Django

I have a static directory inside my apps folder. I added this line of code to my projects settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
The file continues to result in a 404 not found. I'm getting stuck, what should I do?
It probably isn't mistake in your static files settings. But to be sure the relevant settings for static folder in app directories is STATICFILES_FINDERS https://docs.djangoproject.com/en/2.1/ref/settings/#staticfiles-finders. But the AppDirectoriesFinder should be present by default so it is probably OK.
Your problem is probably in your URL scheme:
For development purposes make sure you have django.contrib.staticfiles in your installed apps, DEBUG = True and STATIC_URL = '/static/' (or something similar). Then you should be able access the files.
For deploying it is more complicated but essentially you have setup STATIC_ROOT, before starting the server run python manage.py collectstatic command which will copy all static files to one location and then setup your server (Nginx) to serve the files from this folder under /static/ URL. Here is the official documentation https://docs.djangoproject.com/en/2.1/howto/static-files/#deployment
Note: if your app is foo and you have bar file in your static directory under foo app the URL for the file will be /static/foo/bar.

django static files not appearing with virtual environment

So I'm trying to push my website into production, but I have encountered a problem while in the virtual environment, where my static files are not being found (404 errors).
In the settings.py file, I have STATIC_ROOT = os.path.join(BASE_DIR, 'static/').
In the file /etc/nginx/sites-available/django, I have modified the static files location, to look like this:
location /static {
alias /home/user/myproject/static;
}
In the directory /home/user/myproject, I have another directory called static, and inside are several directories holding the actual static files.
i.e. /home/user/myproject/static/shopApp/shop_app.css
or
i.e. /home/user/myproject/static/officeApp/office_app.css
What I am trying to see is if any of my configurations are set up badly. I am very new to django and web development, so I would appreciate any of your help!
EDIT: After doing some research I think I have the same problem as the guy from stackoverflow in this link: Fetching static files failed with 404 in nginx.
I think the second answer might solve my issue but I don't know where to apply the command chown www-data:www-data .
STATIC_ROOT only for prod, static folder for you app or project. PROD static != project static. For example you have 10 apps, like django admin. Without STATIC_ROOT and collectstatic command, you need point to each static folder in each app.
For example
location /static/admin {
alias /home/user/myproject/env../admin/static;
}
location /static/someapp {
alias /home/user/myproject/someapp/static;
}
But Django does a lot of work for you. You need to point to folder, which collects all static from all apps. For example STATIC_ROOT = os.path.join(BASE_DIR, 'static_remote/') and run collectstatic
location /static {
alias /home/user/myproject/static_root;
}
It turns out, that the problem was with my settings.py file.
Specifically, when your still using the django development server with your virtual environment (0.0.0.0:8000), you should have DEBUG set to True, so that it can serve your static files.
Remember! This is only if your using a django development server. For production, it should be set to False.
I got this information from this django article: https://docs.djangoproject.com/en/2.0/howto/static-files/

Serving static admin files for Django 1.5 in a shared hosting environment

I'm trying to get a fresh installation of Django 1.5 running on an Apache-Server. The WebServer is situated on a shared hosting platform called uberspace.de
which means I have no access to the Apache configuration itself I can however write .htaccess files if that's any help at all. Django is deployed via fast-cgi which is working as expected.
Whats not working however is the access to static files on the server like the .css files and graphics for the Django administration interface.
As mentioned in the official docs I used the following command to copy the static Files into my ~/html/static directory.
manage.py collectstatic
And these are the values from my settings.py:
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = '/static/'
All I get is the infamous django 404 page when I try to access any of these Files.
I also followed the 'How to install and deploy Django' guide on my Webhosters Website to the letter. (sorry its only available in german I believe)
I already contacted the webhosters support but they don't know whats wrong.
All the solutions I've come up with so far suggest setting some sort of Alias in the Apache configuration. Which I can not do.
I'm thankful for any ideas you might have.
Try using a full address instead.
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = 'http://www.mysite.com/static/'
Edit: Perhaps you could ask your host to setup /static/ in your Apache config:
sudo nano /etc/apache2/sites-enabled/mysite.com and add:
Alias /static/ /home/bier/html/static/
I've had a situation before where I've had to upload /static/ files manually because of a highly restrictive host (permissions). Perhaps you need to download a copy of django to your desktop and then upload the static admin file set into your /static/ directory manually?
Lastly, have you added the static files to your urls?
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': '/home/bier/html/static'}),
I have more simpler solution. you need to create a directory named, let's say 'x' in "public_html" or similar location from which server serves the files by default.
Then upload all static files in directory x. (this can be done by running collectstatic locally and then upload all contents of directory STATIC_ROOT to x)
Then, change your STATIC_URL and STATIC_ROOT as follows:
STATIC_URL = '/x/'
STATIC_ROOT = os.path.join(BASE_DIR, '../public_html/x') # Path to folder

how to serve static/admin/ and not just static/css?

After doing the python manage.py collectstatic I found out that I can access everything in static/css but not in static/admin. To be very clear:
I go to: http://mysite.com/static/admin/css/login.css and fails
Manually move the folder static/admin to static/css
I go to: http://mysite.com/static/css/admin/css/login.css and works!
Notice the change from /static/admin/css/ to /static/css/admin/css/ in the URLS. This means that this doesn't solve the problem because the templates continue pointing to the first URL.
I know, serving static admin files has been asked 100 times in stackoverflow but I still cannot make it work (and I am not alone for the comments I have read). It seems nobody has mention this weird problem of accessing the static/css folder and static/anyother_folder.
Some extra details:
There is no errors in the collectstatic, there is no problems with the application css's. This is what I have in my settings.py):
MEDIA_ROOT = join(PROJECT_ROOT,'../media')
MEDIA_URL = '/media/'
STATIC_ROOT = join(PROJECT_ROOT,'../static')
STATIC_URL = '/static/'
I also have tried the deprecated ADMIN_MEDIA_PREFIX without any result.
ADMIN_MEDIA_PREFIX = '/static/admin/'
My nginx configuration is simple and clear.
location /static {
alias /home/the_home/where_the_static_is/static/;
}
location /media {
alias /home/the_home/where_the_media_is/media/;
}
I am using the last version of Django (1.4.2).
Puff. So, here it was. I had another folder in nginx redirecting the /static/admin/ to the Django installation in the virtual environment. In the organization, we use a virtual machine template that pre-configure our Django projects. So, revisiting the nginx configuration I found this:
location /static/admin {
alias /path/to/virtualenvs/my_virtualenv/path_to_the_static_admin;
}
Apparently this worked for Django 1.3 but the path changed at some point.