【发布时间】:2017-06-02 07:20:58
【问题描述】:
我有根据我提供的数据过滤对象的视图函数,如果数据库中不存在该过滤对象,它会将对象添加到 DB(我还没有编写添加函数)。如果它已经存在则显示错误。我正在使用 ajax 发布请求从模板中获取数据。
#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"
user,pid,db,chkbox }---- 我从 ajax 发布请求中获取这些数据,
userid, project_id, db_profileid, setasdefaultproject(boolean) }----- 模型字段
当我尝试检查我的 elif 条件时,我在控制台“aaaa”(其他部分)中得到输出。 elif 有什么问题?
【问题讨论】:
-
还是不清楚!请清楚地提及您想要实现的目标?您收到了 ajax 请求,并基于此创建了一个过滤器和
print "FtoT"? -
请清楚地解释问题并发布完整视图。
-
我的 elif 函数是否正确? @拉贾西蒙
-
你可以查看我的 exc。这是一个简单的版本(我不检查你的条件)
标签: django if-statement view