I'm completely stuck with this, I have a simple Model with a File-Field:
class EmailTemplate(models.Model):
template_name = models.CharField(max_length=100, primary_key=True, verbose_name='Template Name')
template_file = models.FileField(upload_to=f'documents/non_voice/email_templates', verbose_name='File',
validators=[validate_txt_extension])
def __str__(self):
return self.template_name
The User is able to upload a file through a form and should be able to download them as reference later on. Upload works like a charm but when I present the file in a django_table - Table it links to it's relative path and download won't work since it tries to serve only the partial file path. Shouldn't Django automatically pre-pend my Media Dir to build the correct path?? (I can't provide a full path as "upload_to" only accepts relative ones!)
Also the standard upload widget is showing it's current path as a link which is also not working when you click on it, so I would assume the issue has to be somewhere else (best guess is my media settings...)
My Media Settings look like this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
LOG_DIR = os.path.join(BASE_DIR, 'logs')
# MEDIA
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
Any ideas?
Ok I found the issue and it shows to again post all details no matter how insignificant :)
The issue is actually not Django but the NGINX its running on, if you want to serve media files you actually have to let nginx know where they are (static was configured, thats why they worked and media not...)
Example:
location /static/ {
root /EDR_ICT/Dev/WSx_Dev/WSx;
}
location /media/ {
root /EDR_ICT/Dev/WSx_Dev/WSx;
add_header Content-Type application/octet-stream;
}
The "add_header" actually prevents nginx from opening text files in the browser!