Tag: OJ userinfo Python
This week's main task is to fix the modify logic bug on the userinfo page and implement the search function logic.
(1) Modify logic bug fixing:
The bug here is that when we do not reset the password, we cannot submit changes according to the previous logic. This logic is incorrect and can be submitted without changing the password, it mainly refers to the modification of if logic judgment.
First look at the Code:
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':{} })
By the way, the last time there was another question: how to determine which button to submit when there are multiple buttons in a form, for example, the current userinfo page has modify and search buttons, so how can we determine whether
Which one is submitted. After checking on the Internet, one solution is to mark different names for different submit, and then if the request. post. if has_key ("modify") is set up, the modify button is submitted. Otherwise
This is the submission of search. This method is more practical.
For the above modify logic, the main problem is to first determine if the password and confirmpassword are the same and are not empty, submit the modification. When the two are different, the error message is displayed. When determining that the two are the same
If it is null, submit the modification. This is in line with the actual situation.
(2) Implementation of userinfo page search:
As predicted last week, a user's search is implemented on the userinfo page. In fact, the search principle is similar to that of problemlist. When there are multiple buttons in a form, it is a logical judgment.
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':{} })
When we determine a request from search, we first use request. Post ["idname"] to obtain the input query conditions from the page. Initialize the user object set and then use fuzzy query (attribute name _ contains = Query
Note that the underline is composed of two underscores. Then, when we obtain a set that meets the query requirements, it is passed back to the page. Then, the page displays:
<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>
Here, a table is generated to store the results, and a {% for u in users %} result is displayed cyclically. A table is generated. The current design is to click a display item and adjust it to the userinfo page.
But there is no query result.
Then the main task of this week is just a little bit of code. This week's progress is a little slow ........ We hope that the subsequent tasks will catch up.