我有查看功能,根據我給予的數據過濾對象,如果該過濾對象不存在於數據庫中,它將對象添加到數據庫(我沒有寫入添加功能) 。如果它已經存在,則顯示錯誤。我使用ajax post請求從模板獲取數據。elif條件在django視圖
#view.py
@csrf_exempt
def setUserInDB(request):
if request.method=="POST":
if request.POST.get('pname','u_id'):
pname = request.POST.get('pname')
u_id = request.POST.get('u_id')
user = userprofile.objects.get(pk=u_id)
pid = Project.objects.get(title=pname)
else:
u_id = None
pname = None
if request.POST.get('db_id','chkbox'):
db_id = request.POST.get('db_id')
db = Db_profile.objects.get(pk=db_id)
chkbox = request.POST.get('chkbox')
print chkbox
else:
db_id = None
chkbox = None
if Projectwiseusersetup.objects.filter(userid=user,project_id=pid,
db_profileid= db,setasdefaultproject=chkbox):
print "already exist"
elif (((Projectwiseusersetup.objects.filter(userid = user,project_id =
pid,db_profileid=db,setasdefaultproject=False)).exists()) and
(chkbox==True)):
print "FtoT"
elif Projectwiseusersetup.objects.filter(userid = user,project_id =
pid,db_profileid=db,setasdefaultproject=True) and chkbox==False:
print "TtoF"
else:
print "aaaa"
用戶,PID,分貝,chkbox} ----我得到從AJAX請求後,
用戶ID,PROJECT_ID,db_profileid,setasdefaultproject(布爾值)}這些數據-----模型字段
當我試圖檢查我的ELIF條件時,我在控制檯「aaaa」(else部分)中獲得輸出。 elif有什麼問題?
仍不清楚!請明確提及你想達到的目標?你正在得到ajax請求,並基於你創建了一個過濾器和'print「FtoT」'? –
請清楚解釋問題*併發布完整視圖。 –
我的elif函數是否正確? @Raja Simon – Aishu