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 :
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)
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.
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.