【问题标题】:I want save data in sqlite Database, My Query not working in webpage But same code wroking fine in Django Shell我想将数据保存在 sqlite 数据库中,我的查询在网页中不起作用但相同的代码在 Django Shell 中运行良好
【发布时间】:2026-02-01 03:20:13
【问题描述】:

我正在尝试通过 Views.py 添加一些数据,但它不工作,但相同的代码在 django shell 中工作。

我的代码:

username = request.POST['username']
bp = request.POST['bp']
bs = request.POST['bs']
height = request.POST['height']
weight = request.POST['weight']
temp = request.POST['temp']
datasaved = PatTest(Patient= Patient.objects.get(username=str(username)), BS=int(bs), BP=int(bp), PatHeight=float(height), PatientWeight=float(weight), BMI=BMICal(height, weight), TEMPA=int(temp))
            print("Test")
datasaved.save()

【问题讨论】:

  • 你得到什么错误?
  • 看起来request.POST 字典没有包含所有必要的数据。
  • @ruddra 它没有产生任何错误
  • 我确实尝试了所有必要的数据,但没有工作
  • 试试your_var = request.POST.get('whateverdata', False) print(your_var)。如果显示False,则说明您的请求或表单存在问题。

标签: python django sqlite django-models


【解决方案1】:

终于解决了这个问题。问题是 BMI=BMICal(height, weight) 。我只用BMICal(float(height), float(weight))

因为它是模型类中的浮点值所以我刚才提到了它

username = request.POST['username']
            bp = request.POST['bp']
            bs = request.POST['bs']
            height = request.POST['height']
            weight = request.POST['weight']
            tempsd = request.POST['temp']
            print(Patient.objects.get(username=str(username)).PatID)
            datasaved = PatTest(Patient= Patient.objects.get(username=str(username)), BS=int(bs), BP=int(bp), PatHeight=float(height), PatientWeight=float(weight), BMI=BMICal(float(height), float(weight)), TEMPA=int(tempsd))
            datasaved.save()

【讨论】:

    最近更新 更多