【问题标题】:How I save data on database table from django?如何从 django 将数据保存在数据库表中?
【发布时间】:2020-09-15 18:32:30
【问题描述】:

我创建了一个 django 网站,医生和患者都可以在其中注册。患者标记他们的问题,它将发送给他/她想要的医生,就像在 Quora 上提问一样。而且我还想将数据保存在db中

但是当患者插入数据时,我的网页没有保存在数据库中。

Models.py

class patients(models.Model):
    fever=models.BooleanField()
    others=models.TextField()

Views.py

from django.shortcuts import render, redirect
from .models import patients
def patient(request):
    if request.method == 'POST':
        fever=request.POST['fever']
        others=request.POST['others']

        patient_details=patients(fever= fever,others = others)

        patient_details.save()

        return render(request, "patient")
    else:
        return render(request, "patient.html")

Patient.html

<form action="patient">
<label for="exampleFormControlTextarea1"><u>Mark Your Problems</u></label><br>
<!-- FEVER -->
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
  <label class="form-check-label" name="fever" for="inlineCheckbox1">Fever     </label>
</div>

<!--Others-->
<h6 style="text-align:left;"><u>Others</u></h5>
      <div class="form-group">
        <textarea class="form-control" name="others" id="exampleFormControlTextarea1" rows="1"></textarea>
      </div>
      <button class="btn btn-lg btn-primary btn-block" type="submit"> SUBMIT </button>
</h6>



</form>

我如何将这些数据存储在数据库中?

【问题讨论】:

    标签: html django postgresql django-models django-views


    【解决方案1】:

    试试这条线

    patient_details=patients.objects.create(Fever= fever, others = others)
    

    而不是:

    patient_details=patients(Fever= fever, others = others)
    

    【讨论】:

    • 同样的事情来了
    • 您是否在控制台中收到任何错误或任何消息。尝试打印你认为的发烧值和其他值。 Print(fever),print(others) 看看你是否真的得到了视图中的值。
    • 在 html 输入字段中也包含 name 属性
    • 我认为我不应该在 Cheakbox 模型中使用 BooleanField。你能告诉我应该用什么
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 2011-02-19
    • 2016-08-09
    • 2017-03-06
    • 2021-02-22
    • 2018-03-16
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多