Using Django static() to generate audio files' URLs in production

ساخت وبلاگ

Vote count: 0

I'm trying to get my Django app to play audio files (supposedly) uploaded by users via a form. Said files are tied to a model :

# models.py
class Doc(models.Model): # ... tape = models.FileField()

The uploading and saving parts are working fine, and the files are stored where they should be :

- djangoproject | - docapp | - media <- here

So, in order to get where I want, I added these two lines to the settings.py file MEDIA_ROOT = os.path.join(BASE_DIR, 'docapp/media/') and MEDIA_URL = 'docapp/media/'.

I hoped to be able to link to the audio files thus:

# templates/docapp/index.html
...

Unfortunately, this wasn't working because the link generated by doc.tap.url (http://localhost/docapp/media/filename.aac) was returning a 404 error.

After a lot of googling I found this answer, which I happily copy-pasted into my app ... and it worked.

The problem is that I'm not comfortable with inserting code in my apps that I don't understand. I did some research about the static() function and all I could get is this :

Helper function to return a URL pattern for serving files in debug mode

Does this mean that the static function should not be used in production? If so, what should be using in production? And what exactly does it do?

Thanks in advance!

asked 29 secs ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 243 تاريخ : دوشنبه 11 ارديبهشت 1396 ساعت: 22:11