0

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

Celoo
  • 3
  • 5
  • `hasil_rekom = request.POST("hasil_rekomendasi")` Did you mean to type `request.POST.get("hasil_rekomendasi")` ? – Jessie Dec 31 '21 at 06:59
  • @Jesse i tried request.POST.get("hasil_rekomendasi") the page is loaded, but the result isn't show up. i think there's something wrong with updatetable function.. – Celoo Dec 31 '21 at 07:00
  • Unrelated, but you can simplify the `formdata` function by using a ModelForm. You create a ModelForm class that points to your `DataResponden` model, and then you can load in the data easily e.g. `data_resp = DataRespondenForm(request.POST)` https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/#the-save-method – Jessie Dec 31 '21 at 07:01
  • `return redirect('hasil')` Redirect is always a GET, so `request.POST` will be empty in your `hasil` view. Assuming you are just saving an object and then redirecting to a page to view it, you could configure your `hasil` URL to include an `id` and then pass that to your view function. Then when you perform the redirect in `formdata`, include the `id` as an arg e.g. `redirect("hasil", id=data_resp.pk)` – Jessie Dec 31 '21 at 07:08
  • https://docs.djangoproject.com/en/4.0/topics/http/urls/#example – Jessie Dec 31 '21 at 07:08
  • @Jesse thanks! i'll try that formdata later after this hasil works.. i tried using GET but there's an error : MultiValueDictKeyError at /hasil.html – Celoo Dec 31 '21 at 07:37
  • update : the data failed to updating.. that's why the result isn't showing up, not because the views.py – Celoo Dec 31 '21 at 13:41

0 Answers0