【发布时间】:2015-10-20 20:28:12
【问题描述】:
我正在使用 Django 构建一个学生管理系统。
在此代码中,用户使用加密查询搜索学生
name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber,
使用decrypt() 方法提取。
这里有两种方法,一种处理查询,另一种显示学生资料。
查询中的数据不会保存到数据库中,但将用于从数据库中查询学生详细信息。
def process_query(request):
# process the query from the url /?details={{ some hashes here }}
if request.method == 'GET':
raw_deatils = request.GET.get('details', None)
if raw_deatils:
details = decrypt(raw_deatils)
# decrypt is a function that is defined
# in the utils which takes the input string,
# check predeifined tests to test if valid.
# and return the decrypted query string else None
if details:
# now the decrypted message looks something like this.
# name=StudentName&grade=Grade&id=StudentID&phone=
# ParentPhoneNumber&report=StudentReportNumber
# some more processing pulls out value to variables,
name = details['StudentName'],
grade = details['Grade'],
student_id = details['StudentID'],
phone = details['ParentPhoneNumber'],
report = details['StudentReportNumber'],
search_token = details['token']
return redirect("somewhere I'm stuck")
else:
# encryption error, so redirect user to query page
else:
# error with submission redirect to query page
else:
# error with method. redirect to homepage.
def student_profile(request, name=None, grade=None, student_id=None):
# token to be added??
# some data processing to get marks,
# progress report. etc
if student_id:
context = {
'name' : name,
'grade' : grade,
'student_id' : student_id,
'report' : report,
'marks': {
# another dictionary of dictionaries
# as the product of the processing
},
'token' : token,
'attendance': {
# another dicitonary of stuff.
}
else:
context = {
'name' : name,
'grade' : grade,
}
return render(request, 'students/profile/single.html', context)
网址,
url(r'^go/$', 'students.views.process_query' name='process_view'),
url(r'^profile/(?P<name>[a-zA-Z]{1,20})/(?P<grade>[a-zA-Z]{1,20})$',
'students.views.student_profile', name='profile_view'),
当profile_view 被调用时没有'process_view',只应该显示名称和等级。如果profile_view 由process_view 发起,则应呈现出勤率和分数的上下文。
这在process_view 重定向之前一直有效,但我不知道我应该在哪里重定向(或者我什至应该重定向?卡住)并调用profile_view。
所以问题的总结,
如何从process_view 重定向到profile_view 而不会丢失在process_view 中收集的数据到profile_view 并使用profile_view 的url 呈现内容?
我不希望在网址上显示 token 和 student_id。
感谢您的任何建议/帮助。
【问题讨论】:
标签: python django django-views django-urls