Reputation: 502
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
Upvotes: 2
Views: 925
Reputation: 476493
Your PicUpForm
is not a ModelForm
, hence it will not take the Meta
into account at all. Note that the fields should be a list of strings, so you should rewrite the form to:
class PicUpForm(forms.ModelForm):
class Meta:
model = PicUpClass
fields = ['picture']
Note that in your view, for the redirect(..)
you need to pass the name of a view, so:
def add_image(request):
if request.method == 'POST':
form = PicUpForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return redirect('name-of-view')
else:
form = PicUpForm()
return render(request, 'sample.html', {'form': form})
Here you need to replace name-of-view
with the name of a view.
If the view contains a parameter, you can pass this as a named parameter, for example here if your view has a picture_id
parameter (if it has another parameter, then of course you should change the name of the parameter):
def add_image(request):
if request.method == 'POST':
form = PicUpForm(data=request.POST, files=request.FILES)
if form.is_valid():
picup = form.save()
return redirect('name-of-view', picture_id=picup.pk)
else:
form = PicUpForm()
return render(request, 'sample.html', {'form': form})
Upvotes: 2