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
Related
I have deployed a Django project on Ubuntu 16.04 with Nginx and Gunicorn. I have gotten everything, including the static files but my media files will not serve properly.
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
models.py
resume_upload = models.FileField(blank=False, upload_to='resumes', null=True, validators=[FileExtensionValidator(allowed_extensions=['pdf']), validate_file_size])
What I have listed in /etc/nginx/sites-available/ is
server {
listen 80;
server_name website.com www.website.com ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/syed/btawebsite;
}
location = /media/ {
root /home/syed/btawebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/syed/myproject.sock;
}
}
I would like go on the admin, click on the uploaded file and be able to view the file in the browser. Anyone have any ideas on how I can achieve this? I have verified through using the terminal and looking through the directories that the files are in fact adding to ~btawebsite/media/resumes but I am unable to view them when I click the admin url.
When I click on that url I get an error:
Not Found
The requested resource was not found on this server.
Update:
I have changed settings.py to
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
And nginx settings to
location /static/ {
alias /home/syed/btawebsite/static/;
}
location = /media/ {
alias /home/syed/btawebsite/media/;
}
Static is still working, however, media is not.
For nginx configuration, the "=" sign after location means this is the exact location match. So change your settings to
location /media/ {
root /home/syed/btawebsite;
}
and same for /static/. It's correct for the favicon. For more about the nginx location directive, check this.
when I access to my admin dashboard in Django, it doesn't load css and img files, so doesn't work well.
All the files are in 'project/static/admin', and in my settins.py I have this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
When I execute collectstatic, appears: 0 static files copied to '/opt/myenv/myenv/static', 318 unmodified.
I'm using Nginx + Gunicorn, also I'm using https with Cloudflare.
Can anyone help me?
Thanks.
You run collectstatic with STATIC_ROOT set up
If you do not have this in your settings.py , add it
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
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
I am trying to upload an image to the server. No matter what I do, it doesn't appear to properly upload the image. Here is my field in the model:
image = models.ImageField(upload_to='uploads/images/staff', verbose_name='Staff Member Photo', help_text='Required Dimensions: Square, about 275px height/width')
Here are my settings in the settings.py file:
STATIC_URL = "/static/"
STATIC_ROOT = 'static'
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = 'static/media/'
I can't figure out what's going on--it's driving me crazy!!
You should use the absolute path in the MEDIA_ROOT property. For example:
MEDIA_ROOT = '/var/www/mysite/static/media/'
BTW, what error you get then uploading the image? Is it a path-related problem?
I'm using wysiwyg redactor for admin page. So I can add some images to my articles. Firstly, In settings.pyI wrote:
REDACTOR_UPLOAD = '/media/uploads/'
MEDIA_ROOT = '/media/'
MEDIA_URL = '/media/'
in this case, all uploaded images are in C:\media\uploads. It's working.
But I need images to be located in project folder. So I write:
REDACTOR_UPLOAD = os.path.abspath('/media/uploads/')
MEDIA_ROOT = os.path.abspath('media')
MEDIA_URL = '/media/'
Then image location: src="/media/C%3A/virtenvs/web/src/mysite/media/uploads/CAM00415.jpg"
But when debugging settings.py, MEDIA_ROOT = 'C:\\virtenvs\\web\\src\\mysite\\media'
Why?
You put a relative path in MEDIA_ROOT and an absolute path in REDACTOR_UPLOAD. Compare the following two in an interactive console:
>>> os.path.abspath('media/')
C:\\Users\\<username>\\media
>>> os.path.abspath('/media/')
C:\\media
C:\Users\<username> is the current working directory here. Unless you use an absolute path (starting with a /), the path in abspath will get appended to your current directory. The working directory of django seems to be C:\virtenvs\web\src\mysite, which also appears to be your project's directory, so in this case using a relative path for both settings should work:
REDACTOR_UPLOAD = os.path.abspath('media/uploads/')
MEDIA_ROOT = os.path.abspath('media/')
MEDIA_ROOT must be an absolute filesystem path, example:
MEDIA_ROOT = 'C://media/'
to make it inside project folder:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media').replace('\\','/')