2

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

3
  • 1
    It should be a ModelForm, so PicUpForm(forms.ModelForm): Fruthermore please use a string as field, so fields = ['picture']. Commented Jan 21, 2020 at 8:57
  • tried this now getting ``` NoReverseMatch at /picup Reverse for '' not found. '' is not a valid view function or pattern name. ``` Commented Jan 21, 2020 at 9:00
  • that is due to the return redirect(""), you should pass the name of a view. Commented Jan 21, 2020 at 9:00

1 Answer 1

2

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})
Sign up to request clarification or add additional context in comments.

5 Comments

no luck. now getting the error NoReverseMatch at /picup Reverse for '' not found. '' is not a valid view function or pattern name.
hey @willem can u please help me with more one thing . at the line return redirect('name of view') i also want to send a parameter containing the value of inserted record picture field that contains the path. i want to access that path in another view function and template. please can u??
@AayushGupta: see edit: if your view for example has a picture_id parameter, you can use a named parameter.
but i want to access the "picture" field value not the id field
@AayushGupta: then it is pickup.picture. But you can not "encode" a picture in a URL. You should encode something that has an encoding (like a primary key, slug, etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.