如何将变量从一个视图传递到另一个视图并使用Django中的最后一个视图URL进行渲染?

时间:2015-07-29 17:45:03

标签: python django django-views django-urls

我正在使用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'),

如果在没有&#39; process_view&#39;的情况下调用profile_view,则只应显示名称和等级。如果profile_viewprocess_view启动,则应出现上下文和标记。

直到process_view重定向一直有效,但我不知道应该在哪里重新定位(甚至应该重定向?卡住)并调用{{1 }}

所以问题的摘要,

如何从profile_view重定向到process_view,而不会将profile_view中收集的数据丢失到process_view,并使用profile_view的网址呈现内容? 我不希望profile_viewtoken显示在网址上。

感谢您的任何建议/帮助。

2 个答案:

答案 0 :(得分:8)

要访问token中的student_idprofile_view个变量,您可以使用request.session.

process_view中,在会话中设置tokenstudent_id

def process_view(..):
    ...
    request.session['token'] = token # set 'token' in the session
    request.session['student_id'] = student_id # set 'student_id' in the session
    ..

然后在profile_view中,您可以从会话中访问这两个变量。您不需要在URL中传递这两个变量。

def profile_view(..):
    ...
    token = request.session['token'] # get 'token' from the session
    student_id = request.session['student_id'] # get 'student_id' from the session
    ..

您也可以在profile_view中可能需要的会话中设置其他变量。

答案 1 :(得分:1)

不要考虑意见,思考代码

def _student_profile(*arg_data, **kwarg_data):
    context = do(arg_data, kwarg_data)
    return render("my_template", context)


def student_profile(request, name=None, grade=None, student_id=None):
    data = do_things(request)
    data.update({"name": name, "grade": grade, "student_id": student_id})
    return _student_profile(**data)


def process_query(request):
    data = do_other_things(request)
    return _student_profile(**data)