Django CDN with Nginx and uWSGI - django

I having difficulty serving static files with Django 1.10, uWSGI and Nginx.
I have an index.html file, which contains CDN's.
The Django documentation, which is here says to "transfer the static files to the storage provider or CDN." What does that mean, "transfer to the CDN"? Isn't the CDN where you get files from?
settings.py contains,
STATIC_URL = '/static/'
STATIC_ROOT = 'nonAppBoundStaticDirectory'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Running
$ python manage.py collectstatic
does this place all CDN's in my directory 'nonAppBoundStaticDirectory'?
If so then how do i use that in the template?
Excerpt from index.html
<!-- Bootstrap -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.min.css">
Excerpy from /etc/nginx/nginx.conf
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name example.com; # substitute your machine's IP address or FQDN
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ofey/djangoForum/fileuploader/uploaded_files; # your Django project's media files
}
location /static {
alias /home/ofey/djangoForum/noAppBoundStaticDirectory; # your Django project's static files
}
........
Thanks,

It might be just a spelling mistake. In your settings.py you have an extra n in the directory name.
STATIC_ROOT = 'nonAppBoundStaticDirectory'
In your nginx config, you have a different spelling.
location /static {
alias /home/ofey/djangoForum/noAppBoundStaticDirectory;
Make sure that the path point to the exact same directory. It's can be a good idea to use absolute paths in both cases.

Working in development server
If you are working in development server- remove STATIC_ROOT from settings.py. Your settings.py should now look like this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Production
If you are working in production- remove STATICFILES_DIRS from settings.py. Your settings.py should now look like this:
STATIC_URL = '/static/'
STATIC_ROOT = 'nonAppBoundStaticDirectory'
And don't forget to run:
python manage.py collectstatic

Related

Django admin interface missing css styling in production

The user interface is working well, and all CSS styling and static files are served correctly, but the admin interface is missing CSS styling. I looked at similar posts but in those posts people had the issue with both the user and the admin interface. My issue is only with the admin interface.
Please see my static file settings below from settings.py:
STATIC_URL = '/static/'
#Location of static files
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And this is my nginx configuration:
server {
listen 80;
server_name MY_SERVER_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/MYUSERNAME/myproject;
}
location /media/ {
root /home/MYUSERNAME/myproject;
}
I already executed python manage.py collectstatic on the server and got this message:
0 static files copied to '/home/MYUSERNAME/myproject/staticfiles', 255 unmodified.
I restarted nginx after that and also tried emptying my browser cache, but the issue persisted.
More info as requested by #Omar Siddiqui.
Using Django 3.2
My mysite/urls.py contains:
from django.contrib import admin
from django.urls import path, include
# Imports to configure media files for summernote editor
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('qa.urls')),
path('summernote/', include('django_summernote.urls')),
path('chatbot/', include('chatbot.urls')),
]
# Enable media files for summernote editor
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Could you please try below steps and let me know if it's working or not?
Apply below changes in settings.py file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Remove below line from your settings.py:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
Execute below command in production:
python manage.py collectstatic
Update nginx file like below one:
server {
listen 80;
server_name MY_SERVER_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
autoindex_exact_size off;
root /home/MYUSERNAME/myproject;
}
location /media/ {
autoindex on;
autoindex_exact_size off;
root /home/MYUSERNAME/myproject;
}
}
Explanations:
STATIC_ROOT is the folder where static files will be stored after
using python manage.py collectstatic
STATICFILES_DIRS is the list of folders where django will search for additional static files aside from the static folder of each app installed.
In this case our concern was Admin related CSS files that why we use STATIC_ROOT instead of STATICFILES_DIRS
Try changing STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') to:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Then re-run python manage.py collectstatic. This has worked in the past for some people
Try passing an alias to the static location in nginx, like so:
location /static/ {
alias /home/MYUSERNAME/myproject/staticfiles/
}
Don't forget to restart nginx afterwards.

How can I serve Django static files via nginx Docker container?

During development I like to deploy Django static files in an as close to production as possible setup. To achieve this I wrap the Django backend into an image (Dockerfile) and frontend JS and backend Django static files into another image together with nginx configured as webserver (Dockerfile_nginx). The setup is as follows:
File system structure:
<projekt-repo>
/frontend
/backend
/static (generated with python manage.py collectstatic)
settings.py
manage.py
nginx.conf
Dockerfile
Dockerfile_nginx
settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, "backend/static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
nginx.conf:
server {
listen 0.0.0.0:8080;
root /var/www;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 0.0.0.0:8000;
root /var/www/django;
location /static/ {
autoindex on;
alias /var/www/django/static/;
}
}
Dockerfile_nginx:
FROM nginx:1.17.8-alpine
COPY nginx.conf /etc/nginx/conf.d/nginx.conf
COPY edge_frontend/www /var/www
COPY edge_backend/static /var/www/django/static
If I run the application and try to login via the Django admin site (localhost:8000/admin) the site is not styled properly and the log output states
backend | Not Found: /static/admin/css/base.css
backend | Not Found: /static/admin/css/login.css
backend | Not Found: /static/admin/css/responsive.css
backend | Not Found: /favicon.ico
backend | Not Found: /static/admin/css/base.css
backend | Not Found: /static/admin/css/login.css
backend | Not Found: /static/admin/css/responsive.css
backend | Not Found: /favicon.ico
Obviously there is some mismatch in the setup which I am not able to spot right now? Can someone help?
create a folder in your project with the name "static" and then
add in your settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
or you want another path?
You have to set BASE_DIR, STATIC_URL and STATIC_ROOT in settings.py as below...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
And run python manage.py collectstatic

how to serve media files in django app nginx server?

I am testing my django app in production mode (debug=false) using nginx, gunicorn, postgresql.
Though I am able to render static files, I am unable to access files stored in 'media' folder.
In my settings.py following are the variables set:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# also tried another combination: MEDIA_ROOT = 'media'
Also in urls.py the MEDIA_ROOT settings are as follows:
urlpatterns = [
path('admin/', admin.site.urls),
path('venter/', include('appname.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And in my /etc/nginx/sites-available/ file I have the following settings:
server {
listen 80;
server_name website.com www.website.com ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/a/btawebsite;
}
location /media/ {
root /home/a/btawebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/a/myproject.sock;
}
}
However while rendering the xlsx file stored in my django database as follows it throws me NOT found error.
{{file.output_file.url}}
I have tried every combination of configurations required for rendering MEDIA files but unable to achieve the outcome.
Thanks.
UPDATE: following changes to be made in settings.py
MEDIA_URL = '/'
MEDIA_ROOT = 'media'
If everything in django settings is properly configured you just need to add the following in the nginx conf:
location /media {
alias /home/user/django_app/media; #(locaion of your media folder)
}
In your settings.py write like this
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # root for media files
MEDIA_URL = "/media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
FORCE_SERVE_STATIC = True
DEBUG=False
In your urls.py change like this
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
elif getattr(settings, 'FORCE_SERVE_STATIC', False):
settings.DEBUG = True
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.DEBUG = False
In your nginx.conf file change the root to your media folder
location /media/ {
root /home/nazmi/workspace/portal/media/ (url for your media folder);
}
First of all, remove the +static() from your urls.py. That's not correct for production, only for development.
In your nginx configuration, location = /media/ only applies for exact
matches, not locations starting with /media/. Remove the =.

Cannot upload media files in Django production to external folder

I have an issue on my Django production server.
When I try to upload images, they always go to the app/media/ folder.
However I want them to be uploaded to /mnt/data.
In the admin panel, when I upload the image, it is always uploading in the app/media/ folder.
I tried adjusting the Nginx config file and the settings.py, but I guess I am lost.
Here is my Nginx configuration:
location /static/ {
root /home/somthing/something/;
}
location /media/ {
root /mnt/data/;
}
and the Settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
and in my model this is how I create the image:
pictures = models.ImageField(
upload_to='postings/',
verbose_name=_('Posting_picture'),
blank=True, null=True,
validators=[validate_image],
)
I guess following this configuration, the uploaded picture is supposed to be in mnt/data/media/postings.
The media folder on the mnt/data/ is chmod 777, I did it when I lost hope in writing/reading the folder.
Currently you are uploading to:
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
Basically means:
/path/to/project/media
In your case it should be:
MEDIA_ROOT = '/mnt/data'
I finally figured it out, Debendera was right for the path, but the Nginx configuration was wrong. I changed it to :
location /media/ {
alias /mnt/data/;
}
and then it worked. If I am not mistaken, it was better to use alias instead of root.
This is my reference: Nginx -- static file serving confusion with root & alias

Deploy a Django Site with another folder for static files

My enviroment is with Django 1.10.7, PostgreSQL 9.4 and Nginx 1.6 with Gunicorn
I have a global folder called static for common static files to use in sub apps, then i set another folder for production mode with the name 'static_root':
in my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
in my urls.py:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += (static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
urlpatterns += (static(settings.STATIC_URL, document_root=settings.STATIC_ROOT))
when debug is true all work perfect, but in production mode the site not see the static files
i set the location in nginx configuration too
thank!
i found the answer to my question,
the problem was that i set in the nginx configuration: root
location /static {
root /var/projects/project/static_root;
}
the correct way is: alias
location /static {
alias /var/projects/project/static_root;
}