so i'm new to django and python, i'm having a mini project to make a decision support system. so i already build a website, already connect it to the database and have the python code, now i'm having difficulties to push the result/target in python to existing field in table dataresponden from database and print it to html. the flow is like this:
an user input a data (nama, umur, gejala, komorbid) and stored it in table dataresponden(nama, umur, gejala, komorbid, hasil_rekomendasi) in database. user didn't input hasil_rekomendasi because hasil_rekomendasi is the result from the decision support system. in python, i read query dataresponden as dataset, i set hasil_rekomendasi as a target and calculate the remaining column. so i get the result i want named hasil_prediksi, now the problem is i want to push hasil_prediksi to column hasil_rekomendasi in table dataresponden because when user input the data they don't input hasil_rekomendasi. after that i want print it in my html page.
i tried this Execute a python script on button click but didn't work, i tried to use update table but it didn't work too, this is the code
in hasil.html :
<div class="modal-body text-center pb-5" id="hasilrekomendasi">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-8">
<h3 class="portfolio-modal-title text-secondary text-uppercase mb-0">Hasil test</h3>
<div class="divider-custom">
<div class="divider-custom-line"></div>
<div class="divider-custom-line"></div>
</div>
<h5 class="mb-4 text-secondary">{{ hasil_prediksi }}</h5>
<div>
<button class="portfolio-item mx-auto button button4"><a href="{% url 'question' %}">Retake</a></button>
<button class="portfolio-item mx-auto button button4"><a href="{% url 'index' %}">Home</a></button>
</div>
</div>
</div>
</div>
</div>
in nbc.py after doing the nbc i add updatetable. i want to see the last record, after that i want to fill hasil_prediksi to column "hasil_rekomendasi" in table based on the last id in table. where idt = the last id in table from database, hasil_prediksi = the last target in table. this iy my nbc.py :
conn = psycopg2.connect(
host=config.host,
database=config.database,
user=config.user,
password=config.password)
cur = conn.cursor()
datagejala = pd.read_sql_query("SELECT * FROM dataresponden ", conn)
..nbc..
idt = dataid.tail(1)
hasil_prediksi = dftestt.tail(1)
sql_update = "Update dataresponden set hasil_rekomendasi = %s where id = %s"
value = (hasil_prediksi, idt)
try:
cur.execute(sql_update, value)
conn.commit()
print("Data Updated")
except:
print("failed")
this is my views.py
def question(request):
return render(request, 'question.html')
def formdata(request):
nama = request.POST.get("namaa")
umur = request.POST.get("umur")
komorbid = request.POST.get("penyakit_bawaan")
ruang = request.POST.get("ketersediaan_ruang")
demam = request.POST.get("demam")
lelah = request.POST.get("lelah")
batuk = request.POST.get("batuk")
nyeri = request.POST.get("nyeri")
tersumbat = request.POST.get("tersumbat")
pilek = request.POST.get("pilek")
sakit_kepala = request.POST.get("sakit_kepala")
tenggorokan = request.POST.get("tenggorokan")
diare = request.POST.get("diare")
hilang_cium = request.POST.get("hilang_penciuman")
ruam = request.POST.get("ruam")
sesak = request.POST.get("sesak")
sulit_gerak = request.POST.get("sulit_gerak")
nyeri_dada = request.POST.get("nyeri_dada")
data_resp = DataResponden(nama=nama, umur=umur, penyakit_bawaan=komorbid,
ketersediaan_ruang=ruang, demam=demam, lelah=lelah, batuk=batuk,
nyeri=nyeri, tersumbat=tersumbat, pilek=pilek,
sakit_kepala=sakit_kepala, tenggorokan=tenggorokan, diare=diare,
hilang_penciuman=hilang_cium, ruam=ruam, sesak=sesak, sulit_gerak=sulit_gerak,
nyeri_dada=nyeri_dada)
data_resp.save()
return redirect('hasil')
def hasil(request):
hasil_rekom = request.POST.get("hasil_rekomendasi")
hasil_rekomendasi = DataResponden(hasil_rekomendasi=hasil_rekom)
return render(request, 'hasil.html', {'hasil_rekomendasi': hasil_rekomendasi})
but i get an error like "'QueryDict' object is not callable". if i write hasil_rekom = request.POST.get("hasil_rekomendasi") the page shows up but the result isn't. thanks in advance!
update : the file isn't updating in nbc.py that's the problem
hasil_rekom = request.POST("hasil_rekomendasi")Did you mean to typerequest.POST.get("hasil_rekomendasi")?formdatafunction by using a ModelForm. You create a ModelForm class that points to yourDataRespondenmodel, and then you can load in the data easily e.g.data_resp = DataRespondenForm(request.POST)docs.djangoproject.com/en/3.2/topics/forms/modelforms/…return redirect('hasil')Redirect is always a GET, sorequest.POSTwill be empty in yourhasilview. Assuming you are just saving an object and then redirecting to a page to view it, you could configure yourhasilURL to include anidand then pass that to your view function. Then when you perform the redirect informdata, include theidas an arg e.g.redirect("hasil", id=data_resp.pk)