When i update my field i have to replace my upload my image file. i want my image should be attahced like other field in django
I do not want to replace my image file while updating other text and file name should be displayed in upload file choose file section so that as per my need i can modify file. as of now i am facing issues in that i have to again upload file in each update of other field.
image for reference.
eupdate.html
<input type="text" name="institution" id="institution" placeholder="Institution Name" class="form-control w-50 form-row" value="{{pi.institution}}" required>
<input type="text" name="fullname" id="fullname" placeholder="Full Name" class="form-control w-50 form-row mt-1" value="{{pi.fullname}}" required>
<input type="text" name="email" id="contact" placeholder="Personal Email " class="form-control w-50 form-row mt-1" value="{{pi.email}}" required>
<input type="number" name="contact" id="contact" placeholder="Contact " class="form-control w-50 form-row mt-1" value="{{pi.contact}}" required>
<input type="text" name="position" id="position" placeholder="Position " class="form-control w-50 form-row mt-1" value="{{pi.position}}" required>
<input class="form-control w-50 form-row mt-1" type="file" id="formFile" name="upload" value="{{pi.uploaddata.url}}" required>
<img src={{pi.uploaddata.url}} class="img-fluid img-thumbnail" alt="image" style="width:100px; height: 60px"><br>
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" name='checkbox' required>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<br>
<input class="bg-success text-white mt-1" style="margin-top: 0px;" type="submit" />
views.py
def EuAdmin(request, pk):
pi = EmailDb.objects.get(id=pk)
if request.method == 'POST':
institution = request.POST.get('institution', '')
fullname = request.POST.get('fullname', '')
email = request.POST.get('email', '')
contact = request.POST.get('contact', '')
position = request.POST.get('position', '')
uploadd = request.FILES.get('upload', '')
pi.institution = institution
pi.fullname = fullname
pi.email = email
pi.contact = contact
pi.position = position
pi.uploaddata = uploadd
pi.save()
return HttpResponseRedirect("/eadmin")
return render(request, 'NEC/eupdate.html', {'pi': pi})
You should check whether the file has been updated or not, then do things accordingly, if updated then save that one, otherwise old file should be there.
Try this view:
def EuAdmin(request, pk):
pi = EmailDb.objects.get(id=pk)
if request.method == 'POST':
institution = request.POST.get('institution', '')
fullname = request.POST.get('fullname', '')
email = request.POST.get('email', '')
contact = request.POST.get('contact', '')
position = request.POST.get('position', '')
uploadd = request.FILES.get('upload', '')
pi.institution = institution
pi.fullname = fullname
pi.email = email
pi.contact = contact
pi.position = position
if uploadd:
pi.uploaddata = uploadd
pi.save()
return HttpResponseRedirect("/eadmin")
return render(request, 'NEC/eupdate.html', {'pi': pi})