我正在寻找一种更好的方式来代码:Django的:如果在的QueryDict
我的代码是,
@login_required
def updateEmInfo(request):
userProfile = request.user.get_profile()
if request.POST.__contains__('userType'):
userType = request.POST['userType']
else:
userType = None
if request.method == 'POST':
~~~~~~~~
如果我代码userType = request.POST['userType'],
然后我得到如果有错误userType
不等于。
我不认为使用__contains__
方法是个好主意,有没有更好的方法来编写这段代码?
东西容易像下面
userType = request.POST['userType'] ? request.POST['userType'] : None
只要一注:而不是使用'__contains__'你应该使用'in'('如果 '用户类型'在request.POST'中)。但在这种情况下,杰西的答案是准确的。 – mata