how to save file location to database sqlite3

so i want to save file location to database but the file just save it to LOCAL STORAGE. i want using sqlite3 to save the file location. i’m using models and now got error :

here’s my code models:

from django.db import models

from django.core.files.storage import FileSystemStorage

fs = FileSystemStorage(location='/media/mp3')

class Audio_store(models.Model):

    record=models.FileField(storage=fs)

forms.py :

from django import forms

from .models import Audio_store

class AudioForm(forms.ModelForm):

    class Meta:

        model = Audio_store

        fields= ['record']

views.py :

def homepage(request):

    form = AudioForm()

    audio = Audio_store.objects.all()

    if request.method == "POST":

        form = AudioForm(request.POST, request.FILES)

        if form.is_valid():

            form.save()

        return redirect("homepage")

    context={'form':form, 'audio':audio}

    return render(request, "homepage.html", context=context)

urls.py :

urlpatterns = [

    url(r'^admin/', admin.site.urls),

    url(r'^decode/$', views.decode),

    url(r'^$', views.homepage),

    path('audio', views.Audio_store),

]

please help me with fix the code, thank you

The specific error in the code is “no such table”. This doesn’t necessarily indicate anything wrong in your code. This specific error means you didn’t prepare your database for use.

Before running your code, did you run makemigrations and migrate for your app?

(Also see Writing your first Django app, part 2 | Django documentation | Django)

you said i can use django database (so i not make new), how to use it? there’s no change detected when i run makemigrations

The first time you run makemigrations for an app, you need to specify the app name on the command. e.g. python manage.py makemigrations MusicLockApp

See the docs for migrations .

ah i see, it’s work now. now how to get my file again from local storage? and how to see location file at database?

After starting an app with models you have to run

python manage.py makemigrations

then

python manage.py migrate

and the database file will be created under the root folder in your project, but first you must edit

settings.py

in project folder and you will find block of code for database settings as example.

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
	'default': {
		'ENGINE': 'django.db.backends.sqlite3',
		'NAME': BASE_DIR / 'database_name.sqlite3',
	}
}

That’s where these docs will provide you all the information you need.

“Get” a file may mean many different things.

But in general, your view will execute a query to retrieve an instance of your model from the database. Once you have the right model, you can use any of the FileField api methods to perform whatever operation you need.

For example, if you want to include a reference to that file in a page being rendered for the browser, you could use the .url attribute to retrieve the url for that file.