I am trying to upload an image file through a file input from a template. I have followed all the instructions but getting this error when attaching the file and clicking on submit.
AttributeError: 'PicUpForm' object has no attribute 'save'
and hence my image is not uploading to the specified directory as well as the record is not inserting into my sqlitedb
FOLLOWING ARE ALL THE NECESSARY CODES I HAVE USED:
views.py
def add_image(request):
form = PicUpForm()
if request.method == "POST":
form = PicUpForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return redirect("")
else:
return render(request, "sample.html", {"form": form})
forms.py
class PicUpForm(forms.Form):
class Meta:
model = PicUpClass
fields = [model.picture]
picture = forms.ImageField(label='File')
models.py
def upload_to(instance, filename):
now = timezone_now()
base, ext = os.path.splitext(filename)
ext = ext.lower()
return f"C:/Users/Aayush/ev_manage/face_detector/static/img/{now:%Y/%m/%Y%m%d%H%M%S}{ext}"
class PicUpClass(models.Model):
picture = models.ImageField(_("picture"), upload_to=upload_to, blank=True, null=True)
sample.html
{% block content %}
{% load static %}
<form method="post" action="/picup" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button type="submit">submit</button>
</form>
{% endblock %}
urls.py
...
path('picup', views.add_image, name='picup'),
Also i have run the makemigrations and migrate commands after creating the model as required. Please help me as i am a rookie in Python and very importantly need to complete this feature
ModelForm, soPicUpForm(forms.ModelForm):Fruthermore please use a string as field, sofields = ['picture'].return redirect(""), you should pass the name of a view.