So i was using FORM to input file from html to local storage using django (im NOT using database / modals). Now i want get my file again and shown to my html. the file is song (mp3), so i want, the song what i uploaded to local storage can be played in web html. i was already know when you make django project there’ll be automatic database. but i don’t want use it, because i don’t want save my file to database, just save it to LOCAL STORAGE
can i use cookies to get my file and shown to my html? please help me. thank you
here’s my code form.py :
from django import forms
class Audio_store(forms.Form):
password=forms.FileField(widget=forms.FileInput(attrs={'style': 'width: 300px;', 'class': 'form-control',
'text-align' : 'center;'}))
audio=forms.FileField(widget=forms.FileInput(attrs={'style': 'width: 300px;', 'class': 'form-control',
'text-align' : 'center;'}))
models.py (i don’t use it again) :
from django import forms
from django.db import models
class Audio_store(models.Model):
record=models.FileField(upload_to='documents/')
and my views.py :
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.views.decorators.csrf import ensure_csrf_cookie
from .form import Audio_store
from django.forms import formset_factory
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt, csrf_protect
# from .forms import AudioForm
def handle_uploaded_file(f):
with open('mp3/'+f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
def handle_uploaded_file1(f):
with open('txt/'+f.name, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
def homepage(request):
form_class = Audio_store
form = form_class(request.POST or None)
if request.method == "POST":
form = Audio_store(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file1(request.FILES['password'])
handle_uploaded_file(request.FILES['audio'])
return render(request, "homepage.html", {'form': form})
return render(request, "homepage.html", {'form': form})