29๐
I encountered the same problem and was able to fix my nginx configuration by removing the trailing /
from the /static/
location.
location /static { # "/static" NOT "/static/"
# ...
}
- [Django]-Why do I get "ImportError: cannot import name find_spec" when I start a new Django project?
6๐
Try adding the ^~
prefix modifier to your static location to skip checking regular expressions:
location ^~ /static/ {
alias /home/django-projects/tshirtnation/staticfiles/;
}
- [Django]-Machine Learning (tensorflow / sklearn) in Django?
- [Django]-Django Admin nested inline
- [Django]-Auto-populating created_by field with Django admin site
1๐
In your settings.py, put this:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
STATIC_ROOT = "/home/django-projects/tshirtnation/staticfiles/"
STATIC_URL = '/static/'
You donโt need this:
STATICFILES_DIRS = ...
- [Django]-Which Python API should be used with Mongo DB and Django
- [Django]-How to check if something exists in a postgresql database using django?
- [Django]-Django : Table doesn't exist
1๐
settings.py:
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/static/'
MEDIA_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/media/'
MEDIA_URL = '/media/'
In the nginx configurations(/etc/nginx/sites-enabled/default)
server {
server_name localhost;
access_log off;
location /static/ {
alias /home/calosh/PycharmProjects/Proyecto_AES/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Then restart the nginx server:
sudo service nginx restart
And run gunicorn:
gunicorn PFT.wsgi
Serves the application on localhost or the entire local network (on port 80).
http://127.0.0.1/
- [Django]-Django logging on Heroku
- [Django]-How to Unit test with different settings in Django?
- [Django]-Django dynamic model fields
0๐
I think browser tries to find your static in:
http://127.0.0.1:8001/static/
While nginx by default work on 80 port.
You need to define 8001 port in nginx config or run django server on 80 port.
- [Django]-Session data corrupted in django
- [Django]-Django fix Admin plural
- [Django]-What's the proper way to test token-based auth using APIRequestFactory?
0๐
Check this things
1 Whether the static older is accessible by nginx, I mean the folder permission .
2 Or do this
Replace this:
STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'
with this
STATIC_ROOT = โ
And add this in settings
STATICFILES_DIRS = (
'/home/django-projects/tshirtnation/staticfiles/',
)
Donโt forget to reload the nginx server.
Hope this works.
- [Django]-How to create an empty queryset and to add objects manually in django
- [Django]-Chaining multiple filter() in Django, is this a bug?
- [Django]-UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
0๐
Your problem is that the location /
block is always used, even after the location /static/
one, so everything will be proxy_pass
ed.
The solution here is to use some try_files
magic.
server {
server_name 77.241.197.95;
access_log off;
location / {
try_files $uri @django;
}
location /static/ {
alias /home/django-projects/tshirtnation/staticfiles/;
try_files $uri =404;
# here we use =404 because there is no need to pass it to gunicorn.
}
location @djago {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
- [Django]-Django upgrading to 1.9 error "AppRegistryNotReady: Apps aren't loaded yet."
- [Django]-What is the correct way of returning an empty queryset in a Django View?
- [Django]-How to import csv data into django models
0๐
Mine looks like this, and it worked:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), '..', 'static'),
)
And in the nginx configuration, my location /static/ is above the location / like this,
location /static {
//
}
# Finally, send all non-media requests to the Django server.
location / {
//
}
And one more thing, I donโt know if that matters, but I do have a โlistenโ in the server{}. Iโm not sure if it can help.
- [Django]-IntegrityError: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump
- [Django]-What's the purpose of Django setting โSECRET_KEYโ?
- [Django]-Django: How do I override app-supplied urls in my project urlconf?
0๐
try this in settings.py:
import os
ROOT_PATH = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(ROOT_PATH,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
and config file in nginx(or your server setting):
location /static {
alias /home/djangohome/static; # your Django project's static files - amend as required
}
- [Django]-Specify Django Test Database names in settings.py
- [Django]-Macros in django templates
- [Django]-Creating an app on Heroku with Django and NPM
0๐
I ran into this issue and solved it by adding these lines to project urls.py
file:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
] += staticfiles_urlpatterns()
Also there is another reason this kinda issues, and itโs in nginx main configuration file at /etc/nginx/nginx.conf
. Make sure this file contains these lines:
include /etc/nginx/mime.types;
default_type application/octet-stream;
- [Django]-Django SMTPAuthenticationError
- [Django]-Session database table cleanup
- [Django]-Writing a custom management command with args and options โ explanation of fields needed
0๐
Another gotcha to watch for โ be sure to match the trailing slashes. In the example below both the location
and the alias
have trailing slashes. It would also work if neither did, but they must match.
location /static/ {
alias /home/you/django/gtd/staticfiles/;
}
The location has a trailing slash, and the full path is an alias for that, so they both need trailing slashes to stay equivalent. Or you could remove the trailing slash from both.
- [Django]-Base template for all apps in Django
- [Django]-Django: OperationalError No Such Table
- [Django]-Django Import Error: No module named apps
-1๐
Try to remove slash in nginx settings followed by static e.g. It should be โ/staticโ not โ/static/โ, and if your settings were fine then try to reload the server on local machine and try rebooting on remote machine. I faced similar but after rebooting the machine, fixed the issue.
- [Django]-Django Shell No module named settings
- [Django]-Virtualenv(python3.4), pip install mysqlclient error
- [Django]-Choose test database?
- [Django]-Django: show/log ORM sql calls from python shell
- [Django]-Django url pattern โ string parameter
- [Django]-Include intermediary (through model) in responses in Django Rest Framework