尝试列出 html 中的对象时出现 Django TypeError
Django TypeError while trying to list objects in html
我想为模型手动创建表单,但是当我尝试调用 object.all() 方法时,它在 return.
中出现 TypeError 错误
def addPatient(request):
con = Intensivecare_Form.objects.all()
context = {'items': con, 'title': 'Items'}
if request.method == 'POST':
patient = Patient(name=request.POST.get("fname"), data=request.POST.get(
"lname"), Intensivecare_Form=request.POST.get("cform"))
try:
patient.full_clean()
patient.save()
except ValidationError as e:
# Do something based on the errors contained in e.message_dict.
# Display them to a user, or handle them programmatically.
return HttpResponse("Your form is wrong, try again")
return render(request, context, 'AddPatient.html')
我无法对 html 的 return 模型数据进行处理。
TypeError at /api/v1/localmngr/addPatient
join() argument must be str, bytes, or os.PathLike object, not 'dict'
Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1/localmngr/addPatient
Django Version: 3.2.4
Exception Type: TypeError
Exception Value:
join() argument must be str, bytes, or os.PathLike object, not 'dict'
Exception Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/genericpath.py, line 152, in _check_arg_types
Python Executable: /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Python Version: 3.9.6
型号
class Intensivecare_Form (models.Model):
id = models.AutoField(primary_key=True)
hospitals_id = models.ForeignKey(Hospital, on_delete=models.CASCADE)
formname = models.CharField(max_length=128)
data = models.JSONField()
unites_id = models.ForeignKey(Unite, on_delete=models.CASCADE)
class Patient (models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
data = models.JSONField()
intensivecare_forms_data_id = models.OneToOneField(
Intensivecare_Forms_Data, on_delete=models.CASCADE)
Intensivecare_form = models.ManyToManyField(Intensivecare_Form)
def __str__(self):
return f'Id = {self.id}, name = {self.name}'
我检查了错误 html 文件不影响错误。我认为唯一有效的是 object.all() 来自重症监护室。我不知道如何以正确的方式从模型中获取数据到 html。
documentation 中的以下行显示了 render
函数的签名:
render(request, template_name, context=None, content_type=None, status=None, using=None)
你可以看到 template_name
是 second 参数,而 context
是 third 参数,因此下面一行不正确:
return render(request, context, 'AddPatient.html')
相反,它需要是:
return render(request, 'AddPatient.html', context)
我想为模型手动创建表单,但是当我尝试调用 object.all() 方法时,它在 return.
中出现 TypeError 错误def addPatient(request):
con = Intensivecare_Form.objects.all()
context = {'items': con, 'title': 'Items'}
if request.method == 'POST':
patient = Patient(name=request.POST.get("fname"), data=request.POST.get(
"lname"), Intensivecare_Form=request.POST.get("cform"))
try:
patient.full_clean()
patient.save()
except ValidationError as e:
# Do something based on the errors contained in e.message_dict.
# Display them to a user, or handle them programmatically.
return HttpResponse("Your form is wrong, try again")
return render(request, context, 'AddPatient.html')
我无法对 html 的 return 模型数据进行处理。
TypeError at /api/v1/localmngr/addPatient
join() argument must be str, bytes, or os.PathLike object, not 'dict'
Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1/localmngr/addPatient
Django Version: 3.2.4
Exception Type: TypeError
Exception Value:
join() argument must be str, bytes, or os.PathLike object, not 'dict'
Exception Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/genericpath.py, line 152, in _check_arg_types
Python Executable: /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Python Version: 3.9.6
型号
class Intensivecare_Form (models.Model):
id = models.AutoField(primary_key=True)
hospitals_id = models.ForeignKey(Hospital, on_delete=models.CASCADE)
formname = models.CharField(max_length=128)
data = models.JSONField()
unites_id = models.ForeignKey(Unite, on_delete=models.CASCADE)
class Patient (models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
data = models.JSONField()
intensivecare_forms_data_id = models.OneToOneField(
Intensivecare_Forms_Data, on_delete=models.CASCADE)
Intensivecare_form = models.ManyToManyField(Intensivecare_Form)
def __str__(self):
return f'Id = {self.id}, name = {self.name}'
我检查了错误 html 文件不影响错误。我认为唯一有效的是 object.all() 来自重症监护室。我不知道如何以正确的方式从模型中获取数据到 html。
documentation 中的以下行显示了 render
函数的签名:
render(request, template_name, context=None, content_type=None, status=None, using=None)
你可以看到 template_name
是 second 参数,而 context
是 third 参数,因此下面一行不正确:
return render(request, context, 'AddPatient.html')
相反,它需要是:
return render(request, 'AddPatient.html', context)