# how to use cookies to get file from local storage

**URL:** <https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857>\
**Category:** Getting Started\
**Created:** [May 22, 2022, 6:42pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857 "2022-05-22T18:42:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 6:42pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/1 "2022-05-22T18:42:13Z")

</div>

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 :

```auto
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) :

```auto
from django import forms
from django.db import models

class Audio_store(models.Model):
    record=models.FileField(upload_to='documents/')

```

and my views.py :

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 6:45pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/2 "2022-05-22T18:45:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 6:46pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/3 "2022-05-22T18:46:51Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 6:47pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/4 "2022-05-22T18:47:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 6:51pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/5 "2022-05-22T18:51:58Z")

</div>

you can see at the top

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 6:55pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/6 "2022-05-22T18:55:12Z")

</div>

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](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) to see how to create a model and how to use a form to populate that model.

---

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 6:57pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/7 "2022-05-22T18:57:37Z")

</div>

oh sorry, wrong copy files. i was update it

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 7:02pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/8 "2022-05-22T19:02:58Z")

</div>

Now review the docs at:

- [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#filefield)
- [Managing files | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/files/#managing-files)
- [File Uploads | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/http/file-uploads/)  
and this particular section at:
- [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/fields/#filefield-and-fieldfile)  
[/quote]

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.

---

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 7:06pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/9 "2022-05-22T19:06:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 7:09pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/10 "2022-05-22T19:09:42Z")

</div>

> [@tifs73](#):
>
> and your references must make me to made new database.

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.

---

<div class="post-metadata">

**Author:** ![tifs73](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@tifs73](https://forum.djangoproject.com/u/tifs73)\
**Post date:** [May 22, 2022, 7:10pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/11 "2022-05-22T19:10:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2022, 7:11pm UTC](https://forum.djangoproject.com/t/how-to-use-cookies-to-get-file-from-local-storage/13857/12 "2022-05-22T19:11:53Z")

</div>

Start here: [Writing your first Django app, part 1 | Django documentation | Django](https://docs.djangoproject.com/en/4.0/intro/tutorial01/)
