【发布时间】:2021-05-29 14:08:03
【问题描述】:
我还是菜鸟和 Django,我没有遇到这个问题。
我正在尝试向我的所有客户发送一个表单和一个查询集,但是查询集没有返回。
我在 trabalhos\views.py 中执行此操作,查询集来自 clientes\models 中的模型
谁能帮帮我?这真是让我头疼。
谢谢
def novotrabalho_view(request, *args, **kwargs):
if request.method == 'POST':
form = novoTrabalho(request.POST)
if form.is_valid():
codigo = form.cleaned_data['codigo']
nome = form.cleaned_data['cliente']
paciente = form.cleaned_data['paciente']
dispositivo = form.cleaned_data['dispositivo']
tecnico_responsavel = form.cleaned_data['tecnico_responsavel']
c = trabalho(codigo=codigo, cliente=nome, paciente=paciente, dispositivo=dispositivo, tecnico_responsavel=tecnico_responsavel)
c.save()
form = novoTrabalho()
listaclientes = cliente.objects.all()
return render(request, "novotrabalho.html", {'form': form, 'listaclientes': listaclientes} )
【问题讨论】:
-
“未返回查询集”是什么意思?
-
我无法在我的视图中迭代查询集,数据不存在。感谢您的回复。
-
有
clients吗?你能检查数据库是否有cliente记录,它为print(listaclientes)打印什么? -
当然可以。我有几个客户。如果我将 listaclientes 作为 HttpResponse 返回,我得到:cliente 对象 (1)cliente 对象 (2)cliente 对象 (3)cliente 对象 (4)cliente 对象 (5)cliente 对象 (6)
-
到视图或模板。你在这里分享了观点。模板是
novotrabalho.html。
标签: python django forms view model