Using static folding inside app folding Django - 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.

Related

Why Django 4.0 can access static files only with STATICFILES_DIRS, not STATIC_ROOT

When I use STATIC_ROOT:
STATIC_ROOT = BASE_DIR / 'static'
Django cannot access static files and always return 404 error for them.
But when I use STATICFILES_DIRS:
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Everything works well. What's the issue?
I don't understand why Django works in this way, I always thought that STATIC_ROOT is correct way of creating route.
STATIC_ROOT is where the collectstatic command will copy all the static files in.
This folder is meant for production use. So that all the static files in your project (from your apps, or third party apps etc.) are kept in a single folder and therefore becomes easy to serve in production.
By default, Django automatically searches for static files inside each app directory (this is helpful in writing reusable apps). The STATICFILES_DIRS setting allows you to define extra places to look for the static files.
Also, the STATIC_ROOT folder and the STATICFILES_DIRS folder should not be the same values.

Static File and Media Accessing issue while Deploying Django app in Production Environment

While deploying django app on production environment in openlitespeed server, static files and media files not found.
App is woking fine in development environment in local, but after uploading it to server, static content and media files are not accessible.
I have updated my static url as follow:
STATIC_URL = '/public/static/'
STATIC_ROOT = '/home/domain.com/app/public/static
Where app is the project location.
I am getting 404 for error for all static and media files:
Failed to load resource: the server responded with a status of 404 ()
When I try collectstatic I am getting following error on terminal:
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
I tried many options, none of the options worked.
Any solution will be highly appreciated
I assume the missing apostrophe is just missclick here in SO if it worked on development. This is how you should handle STATIC_ROOT in Django >= 4.0:
STATIC_ROOT = BASE_DIR / 'app/public/static'
Older versions:
STATIC_ROOT = os.path.join(BASE_DIR, 'app/public/static')
You are missing inverted comma end of your code
STATIC_ROOT = '/home/domain.com/app/public/static
it should be
STATIC_ROOT = '/home/domain.com/app/public/static'
Problem Solved:
STATIC_ROOT = BASE_DIR / 'app/public/static'
I updated static_root convention according to Django >= 4.0 and on openlitespeed server every static content should be placed inside /public folder which is mandatory rule if we do not edit default configuration. I created /public folder and moved my static and media folder. And then did the collectstatic. Problem resolved.

Django 1.8 Not Serving Static Files in Development

I am using the exact settings as in https://docs.djangoproject.com/en/dev/howto/static-files/ and my app is not loading the files. I've spent way to long trying to solve something so trivial but it's just not working. I've looked at alternatives, but these didn't work either.
This is in my setting file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
This is at the end my urls file:
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I did a collect static when I had the static file dirs so there should be the correct files in my static dir. I also created test files and nothing.
Heres what it will say in the debug view:
Request Method: GET
Request URL: http://localhost:8000/static/assets/stylesheets/style.css
Raised by: django.views.static.serve
Last time I wrote an app this worked fine, i'm not sure why is isn't now.
You haven't used the exact settings in the docs...Which is fine, but you need to at least do the following, if DEBUG = False:
Set up STATICFILES_DIRS. This should be the absolute path to the folder where your static assets live (before they're "collected").
Set up STATIC_ROOT. This is different than the above path, and is where all of your static assets will be collected when you run collectstatic.
Run the collectstatic command to collect (i.e. copy) your assets from your STATICFILES_DIRS to your STATIC_ROOT
Depending on your configuration, and if DEBUG = True, you don't even need to define STATIC_ROOT in development mode.

Setting up django static directory for testing/development

In production, the apache server can handle serving static files just fine. For testing on my local machine I'm using manage.py runserver.
It seems the django apps I have somehow inject some default search paths into this debug server. The browser url shows everything under /static/whatever, but those files are coming from all over the place. This is fine for testing but when moving to production I'll need them in the one path. Thankfully, manage.py collectstatic provides a handy method to copy everything to the one directory given by STATIC_ROOT in settings.py. I've set mine to os.path.join(BASE_DIR, 'static/') where BASE_DIR is where manage.py is.
Now I want to start adding my own stuff in static, but everything's still being hosted by these hidden search directories provided by apps under DEBUG=True. I've tried the following in urls.py:
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
and
import re
from django.conf import settings
if settings.DEBUG:
static_url = re.escape(settings.STATIC_URL.lstrip('/'))
urlpatterns += patterns('',
(r'^%s(?P<path>.*)$' % static_url, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
Neither have worked.
I tried providing my static directory in STATICFILES_DIRS, but get the error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. So then I tried removing my STATIC_ROOT so the path was only given in STATICFILES_DIRS. Finally I can access my files in development (still with staticfiles_urlpatterns from above).
However, I'm pretty sure removing STATIC_ROOT is not the right way to go about it. For a start, I won't be able to run collectstatic after installing other apps without manually changing everything around again. How should I be configuring my static directory for development?
Also, is there a way to turn off hosting app specific static directories so I can make sure my single static is set up correctly (but still be in development mode)?
You're making this more complex than it needs to be. Include "static" in STATICFILES_DIRS as you are doing, for the project-level assets, but make STATIC_ROOT - ie the destination of the collectstatic command - something else, probably "staticfiles", and alias that directory to /static/ in Apache. Django will serve your assets from STATICFILES_DIRS automatically, without you even needing to add the urlpatterns.
OK, I completely missed the point here.
STATIC_ROOT is for collectstatic to pool all the static content for you. You don't add your files to here because it gets overwritten.
Now for production you can get your webserver to host the directory STATIC_ROOT and point STATIC_URL to the url it can be found through the webserver.
STATICFILES_DIRS allows you to add your own directories for collectstatic to search and pool to STATIC_ROOT. Then you can nicely separate static stuff for each of your apps and/or a common project static.
For dev, django expects everything to be in STATICFILES_DIRS and hosts that, not STATIC_ROOT because then there would be duplicates. This makes sense for larger projects because you want to be working on the per-app files, not some blob of stuff for a website.

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