Hello Ken,
Thanks for your brief response, sorry for checking it late due New Year season.
Based on your suggestion from the input here I have updated for the specific output (sorry i only put the specific view function to make the discussion focused on this particular form)
- I have put POST data to the form initialization
- I change the else clause (if is_valid is false) to only print the error
- I change the return to the action page
I need to put some context on how the form will be triggered:
- Here, an admin will enter a page with the yellowed marker
- When the admin hits “TOLAK PENGAJUAN” button (“REJECT APPROVAL in English), it should trigger the erroneous form. Although I have put the initial value (from model query search), i found out the form is loaded but with all initiated forms raised " This field is required.”
Here is the model and the form related to it:
class Pinjamman(models.Model):
Anggota = models.ForeignKey('allpages.Anggota', on_delete=models.CASCADE,related_name='member_id')
amount_borrowed = models.DecimalField(max_digits=12, decimal_places=2)
installment = models.CharField(max_length=2)
date_of_application = models.DateField(null=True,auto_now_add=True, blank=True)
date_review = models.DateField(null=True)
date_rejection = models.DateField(null=True)
date_approved = models.DateField(null=True)
interest = models.FloatField(default=0.01)
information = models.TextField()
status=models.TextField(default='AWAIT REVIEW')
status_reason=models.TextField(null = True)
def __str__(self):
return self.Anggota.last_name
class PinjammanFormReason(forms.ModelForm):
class Meta:
model = Pinjamman
fields = ['amount_borrowed','installment','information','status_reason']
labels = {'status_reason':'Alasan Penolakan','amount_borrowed':'Nominal','installment':'Tenor(bulan)','information':'Tujuan'}
def __str__(self):
return str(self.amount_borrowed)
views.py (focused)
this is the logic if each button is clicked
@login_required(login_url='login/user')
def ActionPinjaman(request,pinjaman_id,anggota_id,status):
s = SessionStore(session_key=request.session.session_key)
u_admin = User.objects.get(id=s['_auth_user_id'])
pinjaman =Pinjamman.objects.filter(id = int(pinjaman_id)).filter(Anggota_id=int(anggota_id)).all()[:1].get()
a = Anggota.objects.get(id = anggota_id)
u = User.objects.get(id = a.user_id)
action_date = datetime.now()
action_delta = action_date+timedelta(days=2)
keputusan = action_delta.strftime('%Y-%m-%d')
action_date_form = action_date.strftime('%Y-%m-%d')
if request.method=='POST':
if status=='UNDER REVIEW':
...
return histori_peminjaman_admin(request)
elif status=='APPROVED':
...
return histori_peminjaman_admin(request)
**elif status=='REJECTED':**
** print('REJECT')**
** return UpdateRejectReason(request,pinjaman_id,anggota_id,action_date_form)**
And updated form view:
@login_required(login_url='login/user')
def UpdateRejectReason(request,pinjaman_id,anggota_id,action_date_form):
s = SessionStore(session_key=request.session.session_key)
u_admin = User.objects.get(id=s['_auth_user_id'])
a = Anggota.objects.get(id = anggota_id)
u = User.objects.get(id = a.user_id)
pinjaman =Pinjamman.objects.filter(id = int(pinjaman_id)).filter(Anggota_id=int(anggota_id)).all()[:1].get()
print(pinjaman.id,pinjaman.Anggota_id,pinjaman.amount_borrowed)
i = {'amount_borrowed': pinjaman.amount_borrowed,
'installment': pinjaman.installment,
'information': pinjaman.information,
'status':'REJECTED'
}
if request.method == 'POST':
form = PinjammanFormReason(request.POST,initial=i)
print(form.is_valid())
if form.is_valid():
print('VALID')
p.status_reason = form.cleaned_data['status_reason']
p.save()
notif = NotifikasiUser()
notif.users=u
notif.messages='Halo{} ,pinjamanmu dengan ID {} ditolak admin dengan alasan {}. Silahkan mencoba kembali pengajuan dengan melengkapi informasi yang diberikan pada alasan.'.format((a.first_name+' '+a.last_name),pinjaman_id,form.cleaned_data['status_reason'])
notif_admin = NotifikasiUser()
notif_admin.users= u_admin
notif_admin.messages='Halo admin, pinjaman dengan ID {} telah ditolak pada {}'.format(pinjaman_id,action_date_form)
notif.save()
notif_admin.save()
context={
'u':request.user,
"pinjaman":p
}
context={
'u':request.user,
'form':form
}
return histori_peminjaman_admin(request)
else:
field_errors = [ (field.label, field.errors) for field in form]
print(field_errors)
context={
'u':request.user,
'form':form
}
return render (request,'allpages/pengajuan_reason.html',context)