OJ2.0userInfo页面Modify逻辑bug修复,search功能逻辑实现

这周的主要任务:userInfo页面Modify逻辑bug修复,search功能逻辑实现。

(一)Modify逻辑bug修复:

这里存在的bug就是在我们不重置password的时候依照前面的逻辑是不能提交改动,这个逻辑是错误的,应该改为能够不改动password也能提交,主要是if逻辑推断的改动

先看一下代码:

def userInfo(request, user_id):
  try:
    user = User.objects.get(userID = request.session['userID'])
  except:
    return HttpResponseRedirect("/index/login")
  other = User.objects.get(userID = user_id)
  if request.method == 'POST':
    if request.POST.has_key("Modify"):
      userID = request.session['userID']
      oldPassword = request.POST['oldPassword']
      password = request.POST['password']
      confirmPassword = request.POST['confirmPassword']
      session = request.POST['session']
      specialty = request.POST['specialty']
      tel = request.POST['tel']
      email = request.POST['email']
      nickname = request.POST['nickname']
      if oldPassword != user.password:
        return HttpResponse("password error")
      else:
        user.password = oldPassword
        user.session = session
        user.specialty = specialty
        user.tel = tel
        user.email = email
        user.nickname = nickname
        if password.strip() != '' and password == confirmPassword:
          user.password = password
          user.save()
          other = User.objects.get(userID = user_id)
          return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'],'user': user, 'other':other})
        else:
          if password != confirmPassword:
            return HttpResponse("password and confirmPassword is not the same!")
          else:
            user.save()
            other = User.objects.get(userID = user_id)
            return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'], 'user':user, 'other':other})
    else:
      users = User.objects.all()
      userid = request.POST['idname']
      try:
        if userid.strip():
          users = users.filter(userID__contains = userid)
        
        #return HttpResponse(users
        return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'], 'user': user,  'other':other, 'users':users, 'idname':userid })
      except :
        #return HttpResponse("fuck")
        return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'],'user': user, 'other':other, 'users':{} })
  else:
    return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'],'user': user, 'other':other, 'users':{} })

对了,上次另一个问题就是在一个表单中有多个button的时候怎样推断是哪一个button的提交,比方当前的userInfo页面有Modify和Search两个button,那么怎样推断究竟是

哪一个的提交呢。在网上查了一下,一种解决方法就是给不同的submit标记不同的name,然后再假设request.POST.has_key("Modify")成立就代表提交的是Modifybutton,否则的

话就是Search的提交了。这样的方法还是比較有用的。

对于上述的Modify的逻辑的问题,主要就是先推断假设password和confirmpassword同样且不空的时候,提交改动。在推断两者不同样的时候错误提示。在推断两者同为

空的时候提交改动。这样就符合实际的情况了。

(二):UserInfo页面搜索实现:

依照上周的估计,是要在UserInfo页面实现一个用户的搜索,事实上原理和problemList的搜索差点儿相同。当我们攻克了一个表单中有多个button的时候,那么就是一个逻辑推断而已。

if request.POST,has_key("Modify") else  运行搜索的过程:

users = User.objects.all()
      userid = request.POST['idname']
      Nickname = request.POST['idname']
      try:
        if userid.strip():
          users = users.filter(userID__contains = userid)
        #return HttpResponse(users)
        return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'], 'user': user,  'other':other, 'users':users, 'idname':userid })
      except :
        #return HttpResponse("fuck")
        return render(request, 'cugbacm/userInfo.html', {'userID':request.session['userID'],'user': user, 'other':other, 'users':{} })

当我们推断是来自Search的请求时,首先使用request.POST["idname"]从页面取出来输入的查询条件。然后初始化user对象集合,然后使用模糊查询(属性名__contains = 查询

条件),注意这里的下划线是两个下划线组成的。然后当我们获得符合查询要求的集合后就传回到页面。再在页面展示:

<div class = "table-responsive" style = "padding-top:58px">
      <table cellpadding = "10" cellspacing = "0" class = "display" id = "example">
        <thead>
          <tr>
           <th>UserId</th>
           <th>Username</th>
          </tr>
        </thead>
        <tbody>
          {%for u in users %}
            <tr class="odd gradeX">
              <td> <a href = "/index/userInfo/{{u.userID}}">{{u.userID}} </a> </td>
              <td>{{u.nickname}} </td>
           </tr>
         {% endfor %}
        </tbody>
      </table>
    </div>

这里生成了一个Table存放查出来的结果,然后一个{% for u in users %}循环展式查询到的结果。就会生成一个表。然后如今的设计是点击某一个展示项的,调到userInfo页

面,只是没有了查询结果而已。

然后这周基本的任务也就这么点代码而已。这周进度有点慢.......。希望后面的任务可以赶上去。


原文地址:https://www.cnblogs.com/bhlsheji/p/4093524.html