404, media files are not served during development

1,743 views
Skip to first unread message

charito.romeo

unread,
Jan 7, 2016, 2:05:33 AM1/7/16
to Django users
Hi ALL, 


I was trying to render user uploaded media files into the template during development but it  throws out a 404 only on the media files. The all other context variables are rendered except the media files. And of course, the video doesn't play, hence the 404. I am using django 1.9.1 and python 2.7.9. 

Here's the 404 result on the terminal:


[07/Jan/2016 06:57:21] "GET /pi_app/video_entry/my-first-video-post/ HTTP/1.1" 200 678


Not Found: /media/video_entries/HALAYONG_BUNTOD_nExLhqF.mp4


[07/Jan/2016 06:57:21] "GET /media/video_entries/HALAYONG_BUNTOD_nExLhqF.mp4 HTTP/1.1" 404 2157





Here's the  settings file. I only included the code relevant to the current issue.

settings.py

....
....

INSTALLED_APPS
= (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'pi_app',
)

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
   'django.middleware.security.SecurityMiddleware',


TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS':[os.path.join(BASE_DIR, 'templates')],
       'APP_DIRS': True,
       'OPTIONS': {
           'context_processors': [
               'django.template.context_processors.debug',
               'django.template.context_processors.request',
               'django.contrib.auth.context_processors.auth',
               'django.template.context_processors.i18n',
               'django.template.context_processors.media',
               'django.template.context_processors.static',
               'django.template.context_processors.tz',
               'django.contrib.messages.context_processors.messages',
               
           ],
       },
   },
]



STATIC_URL = '/static/'

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

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

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


Here's is 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.UserMainView.as_view(), name='index'),
   url(r'^upload_file/$', views.upload_file, name='upload_file'),
   url(r'^video_entry/(?P<video_slug>[\w-]+)/$', views.video_entry, name='video_entry'),
   

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Here is my models.py

class Entry(models.Model):
    VIDEO_ENTRY_TYPE = (
        ('TALK/PRESENTATION', 'Talk/Presentation'),
        ('LECTURE', 'Lecture'),
        ....
        ....
    )
    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=150)
    slug = models.SlugField(unique=True)
    ....
    ....


Here's my views.py:

def video_entry( request, video_slug):
    video_details = Entry.objects.get(slug = video_slug)
    context = {'video_details': video_details}
    context['presentation_video']= video_details.video
    context['presentation_title'] = video_details.title
    ....
    ....

    return render(request, 'pi_app/video_page.html', context)


Here's my template.py

{% block presentation_video %}
            
            <video width="600" height="540" controls>
                <source src="{{ MEDIA_URL }}{{ presentation_video }}" type="video/mp4"> 
            </video>
            <p>Title : {{ presentation_title }}</p>
            ...
            ...
        {% endblock %}

What am I missing? I would really appreciate any help solving this issue. Thanks.

Charito Romeo

unread,
Jan 7, 2016, 2:39:35 PM1/7/16
to django...@googlegroups.com
Oh, never mind, I figured it out.
​S​
o I j
​u​
st recently upgraded to django 1.9.1 from django 1.8.3. In 1.8.3, th
ese lines of code for media files are in​
the app's urls.py :

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

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


​When I upgraded to 1.9.1, all media files stopped being served in the template during development. So ​I moved these lines of code from the app's urls.py to the project's urls.py and all my media files are rendered back to the template. Yeeey!


--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b2de9c50-0f76-4c3f-bc61-36f027495a37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages