Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need extra code for Image Field in Django

I have this in Model

image_name = models.ImageField(upload_to='accounts/')

In my view I have

def account_form(request):
    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
                form.save()
                return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = AccountForm() # An unbound form

    return render_to_response('account_form.html', {
            'form': form,
    })

Do I need to do extra coding for saving image or django will do it itself

like image 975
Mirage Avatar asked Nov 28 '22 01:11

Mirage


1 Answers

Also make sure your form enctype is set in the HTML to submit file data:

<form action="..." method="POST" enctype="multipart/form-data">
like image 58
Torsten Engelbrecht Avatar answered Nov 29 '22 15:11

Torsten Engelbrecht