简体   繁体   中英

How to access the files present in the MEDIA_ROOT in Django

I have a file named "user_001.txt" in the MEDIA_ROOT directory. I am trying to get the file name from a View and print it on the console.

Hello and welcome to StackOverflow Naga Satya!

Firstly, let me kindly suggest you take a look at StackOverflow's “How to write a good question” page. It is always nice to write “I need to achieve X. I have tried A and B but only got result Y from that.” and add some code you have tried to use.

Django's MEDIA_ROOT setting only configures where files uploaded to a FileField go. If you need to print a file to console, you need its path relative to where you launch manage.py from. After that it is just standard Python file reading. Check out this GeeksforGeeks tutorial about reading files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM