I have django application and nginx - django

I have django application and nginx, from browser, it access to the file /static.
such as
http://example.com/static/file.png
However my application has files under /staticroot.
http://example.com/staticroot/file.png
it shows the image.
So, I set on nginx.
location /static {
alias /staticroot;
}
However it doesn't appear
My environment is Debug mode.
My settings py is like this,
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(config("PROJ_PATH"), 'staticroot')
How can I fix this?

in /etc/nginx/sites-available/you_project
set
location /static/ {
root /home/your_admin_name/your_project_folder_name;
}
in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

In order to serve staticroot content you've to rename your STATIC_URL='/staticroot/' & in nginx file make changes like this
location /staticroot/ {
alias /home/your_username/project_name;
}
When request comes to /staticroot/somefile Nginx will look for directory named as staticroot in file system and it will serve provided file. So when you define url for your static or media content make sure you've created directory with the same name.
For more info check Serving Static Content on Nginx docs.

Related

Django not updating static css file design in browser when I run collect static command

I wrote css file and then save it all changes happen and when I run collect static command and then I again change css files no changes displayed on browser nothing happen at all.
It may be due to cache problem. So, you can try this on your browser which will reload from start:
Ctrl + R
I would suggest to double check the settings.py if the STATICFILES_DIRS and STATIC_URL is declared there. An example below-
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'static',
]
MEDIA_URL = '/media/'
MEDIA_ROOT = 'media'
Then create a static folder and media folder where manage.py file is located. Then run the python3 manage.py collectstatic. Hope it works.
It is due to catch problem.
ctrl +f5
Is the solution

static file locate issue on cpanel

my project is working fine on local but on live server(godaddy) its not getting static folder. urls are just
admin/
[name='homepage']
login/ [name='login']
^media\/(?P<path>.*)$
url for static not even located here .
my settings.py static settings are
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
which are working on local perfectly . and location for static folder is here in image , just a static folder placed.
got it worked , by using STATIC_ROOT ="/home/hmd2knu4x125/public_html/static/" .
By default Cpanal store all the web file inside public_html folder. So I guess static folder is inside it as well. hmd2knu4x125 was my instance name .

Django not updating static location setting

I have a project that has been running just fine for about 6 months. Static files have been working perfectly, and everything is great. I have my static files located in a folder as so:
/var/www/html/static/
In my settings.py file, I have the static section setup like so:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/static/',
)
This has been working just fine.
However, I now want to move the static folder to a different location. Specifically, I want to move it inside the main project directory. My project is located at /var/www/html/shq/ so I want to have my static directory located at /var/www/html/shq/static/. I moved the folder, then updated my settings.py file to look like this:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/var/www/html/shq/static/',
)
However, it didn't work. The Django project is still referencing the old location.
What am I missing here? Why isn't the Django project using the new location of /var/www/html/shq/static/?
EDIT
This is what the tail end of my settings.py file looks like:
119 STATICFILES_FINDERS = [
120 'django.contrib.staticfiles.finders.FileSystemFinder',
121 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
122 ]
123
124 STATIC_URL = '/static/'
125 STATIC_ROOT = '/var/www/html/collected_static/'
126 MEDIA_URL = '/media/'
127 MEDIA_ROOT = '/var/www/html/shq/media/'
128 STATICFILES_DIRS = [
129 os.path.join(BASE_DIR, "static"),
130 '/var/www/html/shq/static/',
131 ]
You might try doing something like this. I think it returns the list of directories that django looks for to find static files. Might help debugging.
from django.contrib.staticfiles import finders
from pprint import pprint
pprint(finders.find("", all=True))
Also, I may not be fully understanding your scenario, but you might confirm that the STATIC_ROOT is set to the location where you want to serve your static files (where your webserver will serve the files). The STATIC_DIRS setting tells collectstatic where to find static files, but the STATIC_ROOT is where collectstatic will actually place the files.
I figured it out. Not surprisingly, it was an easy fix once I figured it out.
It had nothing to do with my Django settings and everything to do with Apache.
Original
Alias /static/ /var/www/html/static/
So no matter what I did in my Django settings.py file, Apache was overriding that to send /static/ requested to the wrong directory.
New Apache Setting
Alias /static/ /var/www/html/shq/static/
Now the proper static files are being referenced. Hopefully this helps someone else in the future :)

Where are my static files in a Dokku based installation?

I finished a Dokku deployment on a Digitalocean server. My application seems to work, except for my static files.
Here are the relevant parts of my settings.py file:
PROJECT_DIR = Path(__file__).absolute().ancestor(2)
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child('static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = PROJECT_DIR.child('staticroot')
Does someone know how can I point my app at my static files on a Dokku based system?
Though it's not answering my question, we're currently serving the static files from AWS S3, and that works. It makes my question less urgent, but I still would like to know if it is possible to solve it "locally".
You may need to do a few things
run ./manage.py collectstatic to move static files to your static folder
Update your web server config so that /static/ points to the static folder.
Have you done this already?

Static Assets in Django not Finding STATICFILES_DIRS

Here is the error I receive when trying to load {{STATIC_URL}}img/memestatlogo2.jpg. Why is django not looking in STATICFILES_DIRS for img/memestatlogo2.jpg?
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/img/memestatlogo2.jpg
Using the URLconf defined in memestat.urls, Django tried these URL patterns, in this order:
^$
The current URL, static/img/memestatlogo2.jpg, didn't match any of these.
Here are relevant configurations form my settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/')
The image is then located in
'/home/ryan/Programming/OpenCV-2.4.2/msheroku/memestat/stats/img/memestatlogo2.jpg'
STATICFILES_DIRS is only used when you execute the collectstatic command. Files are then copied into STATIC_ROOT which in the example above is unset.
You need to set STATIC_ROOT to a dir where you want your static files to be copied. This dir also holds your project-wide static files that are not part of any app like perhaps robots.txt, favicon.ico, your logo(s) and background images.