how to use cookies to get file from local storage

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})

Using the database does not mean that the file contents go into the database.

The database only saves the location of the file to be accessed at a later time.

The file itself is always stored in local storage.

but how to save the location? before i use form, i use models and got error because i don’t upload it to new database (which is database must i created first)

It’s impossible to diagnose problems without seeing the code. Post your model, form, views, and the complete traceback of the errors being received, and we can work forward from there.

you can see at the top

Your model is defined incorrectly.

You show what you imply your model to be, as inheriting from forms.Form instead of models.Model.

Review the work you’ve done with the Official Django Tutorial to see how to create a model and how to use a form to populate that model.

oh sorry, wrong copy files. i was update it

Now review the docs at:

By hard-coding your file writes, you create the possibility of there being a conflict with file names being uploaded. (If two people upload a file with the same name, the second file will overwrite the first. If the first person then retrieves that file, they will get the file that the second person uploaded.)

So you want to rely upon Django’s management of uploaded files to ensure that the files get unique names as they are stored in the local file system.

that’s why i was try using cookies to get files. and your references must make me to made new database. i don’t want it or can i import models without using django.db?

I do not understand why you keep making this incorrect statement.

You do not need to “make” a new database.

You can use the database you’re already using in Django.

can you make the code what do you mean? maybe it’ll be easy for me to understand and i will tried to using

Start here: Writing your first Django app, part 1 | Django documentation | Django

1 Like