Using Django sessions
i have got some problem on implementing Django sessions. I have an employee listing page with a filter option on it. Filtering is done based on the fields and a text entered in a text box. The users filtered is displayed in the filter.html page; clicking on the user name takes to that user's profile page. Now i want to go back to the previous page (given a back button in the employee profile page) where the filtered users were displayed(filter.html). I guess this could be done by sessions. But i am not sure the way i use sessions is correct. Please help me to solve this as i am new to Django. Will post my code here:
def filter(request):
val=request.POST.get('designation')
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=request.POST.get('textField'))
request.session['session_textField']="textField"
request.session['session_choices']="choices"
session_textField = request.session["session_textField"]
session_choices = request.session["session_choices"]
print session_textField,session_choices
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName=request.POST.get('textField'))
request.session['session_textField']="textField"
request.session['session_choices']="choices"
session_textField = request.session["session_textField"]
session_choices = request.session["session_choices"]
print session_textField,session_choices
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=request.POST.get('textField'))
request.session['session_textField']="textField"
request.session['session_choices']="choices"
session_textField = request.session["session_textField"]
session_choices = request.session["session_choices"]
print session_textField,session_ch开发者_如何学运维oices
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=request.POST.get('textField'))
request.session['session_textField']="textField"
request.session['session_choices']="choices"
session_textField = request.session["session_textField"]
session_choices = request.session["session_choices"]
print session_textField,session_choices
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=request.POST.get('textField'))
request.session['session_textField']="textField"
request.session['session_choices']="choices"
session_textField = request.session["session_textField"]
session_choices = request.session["session_choices"]
print session_textField,session_choices
else:
return render_to_response('filter.html')
return render_to_response('filter.html',{'newData':newData,'val2':val2})
Filter.html
<html>
<h3><br><br>
The Filtered Data
</h3>
<body>
<br>
{%for data in newData%}
<a href ="http://10.1.0.90:8080/singleEmployee/{{data.id}} ">
{{ data.userName}}<br>
{%endfor%}
</body><br><br><br><br>
<a href ="http://10.1.0.90:8080/employeeList/ "> Home Page </a>
</html>
Employee Profile html page
<table>
<tr> <td>Name: {{ empSelect.userName }} </td> </tr><td>
<tr> <td>Designation: {{ empSelect.designation }} </td> </tr><td>
<tr> <td>Employee ID: {{ empSelect.employeeID }} </td> </tr><td>
<tr> <td>Contact Number: {{ empSelect.contactNumber }} </td> </tr><td>
<tr> <td>Project: {{ empSelect.project }} </td> </tr><td>
<tr> <td>Date Of Join: {{ empSelect.dateOfJoin }} </td> </tr><td>
</table></h4><br>
<input type="submit" value="Delete User" onClick="window.location.href='/userDelete/{{empSelect.id}}'"/>
<input type="submit" value="Update User" onClick="window.location.href='/userUpdate/{{empSelect.id}}'"/>
</div><br><br>
<a href ="http://10.1.0.90:8080/filter/{{emp.id}} "> Back </a><br><br>
<a href ="http://10.1.0.90:8080/employeeList/ "> Home Page </a>
</body></html>
If the code i provided has a lot of error, please help me to correct it.
It's not at all clear what you are trying to do. But notice this:
request.session['session_textField']="textField"
request.session['session_choices']="choices"
These two lines, which are identical in every block, simply set the two session variables to the strings "textField" and "choices". I don't think this is what you want to do, although as I say above I don't know exactly what you do want to do. However, this seems to be an elementary programming problem, not anything to do with Django sessions in particular.
精彩评论