Django Db Images video Django Db Images video database database

Django Db Images video


Daniel is referring to the fact that storing large binary files in a DB is not efficient. Use the filesystem instead - take a look at FileField and ImageFileField, which are designed to handle file uploads:

http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield

The only thing stored in the DB is the path to the binary file.


The Model you need models.py

from django.db import modelsclass Video(models.Model):    name= models.CharField(max_length=500)    file= models.FileField(upload_to='videos/', null=True, verbose_name="")

Form class you need forms.py

from .models import Videoclass VideoForm(forms.ModelForm):    class Meta:        model= Video        fields= ["name", "file"]

inside views.py

from django.shortcuts import renderfrom .models import Videofrom .forms import VideoFormdef showvideo(request):    firstvideo= Video.objects.last()    videofile= firstvideo.file.url    form= VideoForm(request.POST or None, request.FILES or None)    if form.is_valid():        form.save()    context= {'file_url': videofile,              'form': form              }    return render(request, 'videos.html', context)

And finally your template: videos.html

<body><h1>Video Uploader</h1><form enctype="multipart/form-data" method="POST" action="">    {% csrf_token %}    {{ form.as_p }}    <input type="submit" value="Upload"/></form><br><video width='600' controls>    <source src='{{ file_url }}' type='video/mp4'>    File not found.</video><br></p></body>


No, there isn't a tutorial on this. Django doesn't support it out of the box, and it's horribly inefficient. Don't do it.