在我的Django项目中,在第一个视图中创建了一个模型实例。然后,在第二个视图中访问它,并更新几个先前为空的字段。在第三种视图中,我试图使用模型实例中包含的信息来运行docusign API。但是,当我以与第二个视图相同的方式查询它时,它返回“ None”。
def firstView(request):
if request.method=='POST' or request.method=='FILES':
form = myObjForm(request.POST, request.FILES)
newObj = myObj(
phonenumber = request.POST.get('phonenumber')#There are several other fields that are occupied, but I pass the "phonenumber" between views to be able to retrieve the same instance between views
)
newObj.save()
return HttpResponseRedirect('pdf/{}'.format(newObj.phonenumber))
#Some other code
def secondView(request, phone_number):
myobj = myObj.objects.filter(phonenumber=phone_number).order_by('id').last() #pull the most recent instance. This does work.
myObj.pdf = 'path/to/pdf.pdf'
myObj.save()
return HttpResponseRedirect('sign/{}'.format(phone_number))
def ThirdView(request, phone_number):
myobj = myObj.objects.filter(phonenumber=phone_number).order_by('id').last() #This is where I get a 'None' type response.
#Do some things with the model
return HttpResponseRedirect('../success/')
def success(request):
return render(request, 'myapp/success.html')
我希望ThirdView中的查询返回与secondView中相同的结果。但是,它不返回模型的正确实例,而是返回“无”。如果我访问管理页面并查看模型,则字段确实在secondView中进行了更新,因为pdf现已存在。当我查看局部变量时,会看到myObj = None
和phone_number = 'success'
。我不确定为什么会这样,也不确定如何解决。我在做什么错了?