Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django get media directories in view or model

I have a model where I I am writing a function to upload files :

def get_upload_file_name(instance, filename):
    return "my_files/{}".format(filename)

In my settings I have

MEDIA_ROOT = os.path.join(BASE_DIR, 'static')

My static structure

static
    css
    img
    js
    my_files

Here I dont want to return hardcoded my_files I want to return from setting like

{{MEDIA_ROOT}}/{}".format(filename) 

or something like that... How can I do this ?

like image 849
varad Avatar asked Nov 30 '25 16:11

varad


1 Answers

To answer the question you actually asked, you can access your MEDIA_ROOT setting (and any other Django settings) in your views and models with:

settings.MEDIA_ROOT

As mentioned however, you probably want to read up on how media is handled in Django first.

Read about using settings in python code in the docs.

like image 177
rnevius Avatar answered Dec 03 '25 05:12

rnevius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!