我在实现Django会话时遇到了一些问题。我有一个员工列表页面,上面有一个过滤器选项。根据字段和在文本框中输入的文本完成过滤。过滤的用户显示在filter.html页面中;单击用户名将转到该用户的配置文件页面。现在我想回到显示过滤用户的上一页(在员工配置文件页面中给出一个后退按钮)(filter.html)。我想这可以通过会议来完成。但我不确定我使用会话的方式是否正确。请帮我解决这个问题,因为我是Django的新手。将在此处发布我的代码:
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_choices
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>
员工资料html页面
<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>
如果我提供的代码有很多错误,请帮我纠正。
答案 0 :(得分:1)
一点也不清楚你要做什么。但请注意这一点:
request.session['session_textField']="textField"
request.session['session_choices']="choices"
这两行在每个块中都相同,只是将两个会话变量设置为字符串“textField”和“choices”。我不认为这是你想要做的,虽然如上所述,我不知道你做什么想要做什么。但是,这似乎是一个基本的编程问题,特别是与Django会话无关。