Problem rendering media files in template during development

57 views
Skip to first unread message

charito.romeo

unread,
Jul 20, 2015, 3:16:52 AM7/20/15
to django...@googlegroups.com
Hi django folks, 

I ran into a hiccup when I was rendering media files in templates during development. I wanted to render the image and the video that was uploaded by the user BUT instead of the contents of the image and the video being rendered,  the name of the file was rendered instead.

I am using python 2.7.9 and django 1.8.3. Here are my  codes relevant to the question I'm asking:

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'

STATIC_PATH = (os.path.join(BASE_DIR, 'static'),
               '/var/www/static/',)

STATICFILES_DIRS = (
    STATIC_PATH,
)
MEDIA_URL = '/media/'




my models.py:

class Entry(models.Model):
    VIDEO_ENTRY_TYPE = (
        ('SEMINAR', 'Seminar'),
        ('LECTURE', 'Lecture'),
        ('TALK', 'Talk'),       
    )
    user = models.ForeignKey(User)
    video = models.FileField(upload_to='video_entries')
    entry_type = models.CharField(max_length=100, choices=VIDEO_ENTRY_TYPE)
    title = models.CharField(max_length=250)


class EntryForm(ModelForm):   
    class Meta:
        model = Entry
        fields = ['video', 'entry_type', 'title']
 

my views.py:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.views.generic import ListView

from . models import User, Entry, EntryForm

class IndexView(ListView):
    template_name = 'pi_app/index.html'
    context_object_name = 'latest_seminars'
    
    def get_queryset(self):
        return Entry.objects.filter(entry_type='SEMINAR').order_by('-pub_date') [:10]
     
    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['latest_lectures'] = Entry.objects.filter(entry_type='LECTURE').order_by('-pub_date') [:10]
        context['latest_talks'] = Entry.objects.filter(entry_type='TALK').order_by('-pub_date') [:10]

my urls.py:

from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static

from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^upload_file/$', views.upload_file, name='upload_file')
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


my template.py:
<!DOCTYPE html>

{% load staticfiles %}

<html>
    <head>
        <title>Homepage</title>
    </head>
    
    <body>
        <h1> Welcome {{ user }} </h1>
       
     <!-- SAMPLE BLOCK THAT I WANT THE MEDIA FILES RENDERED -->
       
        {% block seminar %}
        <section id="latest_seminars">
                
            <h2>Most Recent Seminars</h2>
                
            {% if latest_seminars %}
                <ul>
                    {% for seminar in latest_seminars %}
                        
                        <video width="350", height="250", alt="{{ seminar.title }}" controls >
                            <source src= "{{MEDIA_URL }} {{ seminar.video }}"/>
                        </video>
                        {{ seminar.title }} by {{ seminar.user }}
                    {% endfor %}
                </ul>
            {% endif %}
        </section>        
        {% endblock seminar %}
        


Normally, the video or the image will be rendered. In my case, its not rendered. What is rendered instead is the name of the movie file (media/video_entries/name_of _movie_file)  or the name of the image file  (media/video_entries/name_of _image_file). All the other variables are rendered correctly  except the media files. What am I missing? Please enligthen me. Your help is hightly appreciated.  Thanks.

Charito Romeo

unread,
Jul 20, 2015, 5:14:44 AM7/20/15
to django...@googlegroups.com
Oh, I forgot to include the MEDIA_ROOT = os.path.join(BASE_DIR, 'media') in settings.py.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/66cb760a-96d3-4998-beec-1f6a947b9259%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

charito.romeo

unread,
Jul 22, 2015, 1:20:05 AM7/22/15
to Django users
Never mind. I figured it out. For some reason, {{ MEDIA_URL }} doesn't work  but {{ media_url }} does. Now all my movies and images are rendered. Cheers. :)

ltc.h...@gmail.com

unread,
Jul 22, 2015, 1:29:41 AM7/22/15
to django...@googlegroups.com
Hi Charito,

What tool did you use to insert your python codes into the marked  fields below, i.e., settings.py, my models.py, etc?

Regards,
Hal

Sent from Surface

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Charito Romeo

unread,
Jul 22, 2015, 2:12:14 AM7/22/15
to django...@googlegroups.com
Hi Hal,

I'm using the code icon right on top of the gmail textbox. Click on the { } icon which is last icon on the top right of your text box.

ltc.h...@gmail.com

unread,
Jul 22, 2015, 7:58:32 AM7/22/15
to django...@googlegroups.com
Thank you very much, Charito 😊

Sent from Surface

Reply all
Reply to author
Forward
0 new messages