1

I am trying to create a form without using ModelForm. I using the input elements in HTML for the purpose (to upload name and image). But I am having trouble uploading images with this process. The name is getting saved but not the image. My code:

models.py

class Register(models.Model):
    name = models.CharField(max_length=50, null=True)
    idCard = models.FileField(upload_to='idCard', null=True)

views.py

def index(request):
    if request.method == 'POST':
            data.name = request.POST.get('name')
            data.idCard = request.POST.get('idCard')
            data.save()
            return redirect('/')
    return render(request, 'event/index.html')

index.html

<form class="mform" id="myform" method="POST" id="myform" action="" enctype="multipart/form-data">
    {% csrf_token %}
<fieldset>  
    <legend>Registeration</legend> 
    <table cellspacing="0"><tbody>
    <tr><td>
        <label for="u_name"> Username :</label></td><td>
            <input type="text" name="name" id="u_name">
<td>
</tr>
<tr><td>
    <label for="u_img"> IDCard :</label></td><td>
        <input type='file' accept='image/*' onchange='openFile(event)' name="idCard" id="u_img">
</td></tr>

The name is getting saved but not the image.

Deepjyoti De
  • 117
  • 1
  • 11

1 Answers1

1

The files are stored in request.FILES:

def index(request):
    if request.method == 'POST':
            data.name = request.POST.get('name')
            data.idCard = request.FILES.get('idCard')
            data.save()
            return redirect('/')
    return render(request, 'event/index.html')

That being said, I strongly advise to use a Form (or ModelForm). A form does not just handle saving the object, it also performs proper validation, can return error messages, and removes a lot of boilerplate code. Often with some tweaking, you can let the form look like you want. But even if you manually write the form in the template, you can still use a form at the Django level to validate and save the object.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks. That did the job. Actually I want the id for a jQuery script. `````` this type. How can I have an id for ```{{form.img}}```? ***Help!*** – Deepjyoti De May 27 '20 at 18:58
  • 1
    @DeepjyotiDe: you can specify the `attrs` of the widget, which is a dictionary. Here you thus probalby want `attrs={'id': 'imgid'}`: https://docs.djangoproject.com/en/3.0/ref/forms/widgets/#django.forms.Widget.attrs – Willem Van Onsem May 27 '20 at 19:06
  • Actually I wanted to create a preview page for my form. The users will be directed to a preview page after they fill up the form and hit the next button. I don't want to save the form before they submit from the preview page. In my previous attempt, the form was saved upon hitting the next button and overwritten when the user clicked the submit button in the preview page. Drawback: The form was submitted if the user closed the browser window on the preview page. – Deepjyoti De May 27 '20 at 19:20