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" value="{{pi.institution}}" required>
<input type="text" name="fullname" id="fullname" placeholder="Full Name" value="{{pi.fullname}}" required>
<input type="text" name="email" id="contact" placeholder="Personal Email " value="{{pi.email}}" required>
<input type="number" name="contact" id="contact" placeholder="Contact " value="{{pi.contact}}" required>
<input type="text" name="position" id="position" placeholder="Position " value="{{pi.position}}" required>
<input type="file" id="formFile" name="upload" value="{{pi.uploaddata.url}}" required>
<img src={{pi.uploaddata.url}} alt="image" style="width:100px; height: 60px"><br>
<input type="checkbox" value="" id="invalidCheck" name='checkbox' required>
<label for="invalidCheck">
Agree to terms and conditions
</label>
<br>
<input 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})
CodePudding user response:
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})