play <audio></audio> file in django template

前端 未结 1 602
野性不改
野性不改 2021-01-21 23:13

I\'ve been struggling with this for so long that I\'m bordering depression.

I have a model called \"Song\" that looks like this.

from django.db import mo         


        
相关标签:
1条回答
  • 2021-01-21 23:25

    You should add MEDIA_ROOT and MEDIA_URL configuration. It will be easy to handle things. Here is the solution to your problem.

    In settings.py:

    MEDIA_ROOT=os.path.join(BASE_DIR,"songdir")
    MEDIA_URL='/media/'
    

    Also in settings.py add 'django.template.context_processors.media', in the TEMPLATES option's context_processors.

    In project/urls.py:

    from django.conf import settings
    from django.conf.urls.static import static
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    Then you can simply use:

    {{link.url}} 
    

    instead of hardcoding it in your template file.

    0 讨论(0)
提交回复
热议问题