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

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.

Related

All my static files in Django are not found, but findstatic finds them

I am developing a Django site on an Ubuntu 16.04 LVM using Apache and mod_wgsi. When I try to load one of my pages, all the static files give a 404 not found error. I believe I have my settings set correctly because
python3 manage.py findstatic
is able to find the files and running the development server with DEBUG=off and the --insecure flag, It still works correctly. Does this mean it is an issue with apache?
Here are my static file settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
#'/home/matt/jukebox/bin/jukebox/static/'
)
Does anybody know what could be wrong? The files were being served previously, but after updating my server and the code for the site, it stopped serving.
Thank You
Adding this to my apache config file fixed the issue:
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
I found the answer here:
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
Still not sure why it had been working previously without these settings, but hopefully this answer can help someone.

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/

Django points to different locations for static files

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

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

Confusion with serving static files with Django and fcgi

I thought I had this figured out, but I've read a few different things regarding this and I'm starting to get really confused.
The URL settings in my settings.py look like this:
MEDIA_ROOT = HOME + '/uploads'
MEDIA_URL = PUBLIC_HTML + 'media/'
STATIC_ROOT = HOME + '/static'
STATIC_URL = PUBLIC_HTML + 'static/'
ADMIN_MEDIA_PREFIX = PUBLIC_HTML + 'static/admin/'
Where HOME is /home/username/ and PUBLIC_HTML is like "http://www.mydomain.com/test/myproject/"
I seem to be getting my static files okay, but my media files don't show up. I don't have anything set up in my urls.py file, and I'm using shared hosting, so all I can do is use the .htaccess file for apache directives.
Obviously, I'd like to get my media files to show up, but my first question is why are the static files showing up? It doesn't make sense. I thought that apache had to intercept these requests before they were processed by Django, but they seem to be showing up fine without any kind of django.static.serve business in the urls. How is that possible? Am I doing this right (I'm sure I'm not), and how do I get the media files to show up? Why aren't they showing up when the static files aren't?
STATIC_ROOT must be served be apache on STATIC_URL, which you've done right apparently.
In the same fashion, MEDIA_ROOT must be served by apache on MEDIA_URL.
You could set up an alias, from MEDIA_URL to MEDIA_ROOT for example:
Alias /test/myproject/media/ /home/username/media/
It is preferable to avoid hard coding absolute paths, but first make sure that it works like this.