如何将变量从一个视图传递到另一个视图并在 Django 中使用最后一个视图的 URL 进行渲染? [英] How do I pass variables from one view to another and render with the last view's URL in Django?

查看:14
本文介绍了如何将变量从一个视图传递到另一个视图并在 Django 中使用最后一个视图的 URL 进行渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django 构建一个学生管理系统.

I'm building a student management system using Django.

在此代码中,用户使用加密查询搜索学生name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber,使用 decrypt() 方法提取.

In this code, The user search for a student with the encrypted query name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber, that is extracted with the decrypt() method.

这里有两种方法,一种是处理查询,一种是显示学生资料.

Here are the two methods, the one which process the query and the one which shows the student profile.

查询中的数据不会保存到数据库中,但会用于从数据库中查询学生详细信息.

No data from the query is saved to the database, but will be used to query the student details from the database.

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_viewprocess_view 启动,则应呈现出勤和标记的上下文.

whenever profile_view is called without 'process_view', the name and grade only should be shown. If the profile_view is initiated by the process_view the context with attendance and marks should be rendered.

这一直有效,直到 process_view 重定向,但我不知道我应该在哪里重定向(或者我应该重定向?卡住)并调用 profile_view.

This works till the process_view redirect, but I don't have a clue where should i redierect (or even should I redirect? stuck) and calling the profile_view.

问题的总结,

如何从 process_view 重定向到 profile_view 而不会丢失在 process_view 中收集的数据到 profile_view 并呈现profile_view url 的内容?我不希望 tokenstudent_id 显示在 url 上.

How do I redirect from process_view to profile_view without losing data collected in process_view to the profile_view and render the content with the url of profile_view? I don't want the token and student_id to be shown on the url.

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

Thanks for any suggestions/help.

推荐答案

访问 profile_view 中的 tokenstudent_id 变量,您可以使用 request.session.

To access the token and student_id variables in profile_view, you can use request.session.

在您的 process_view 中,在会话中设置 tokenstudent_id.

In your process_view, set token and student_id in the session.

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 中,您可以从会话中访问这 2 个变量.然后,您不需要在 URL 中传递这两个变量.

Then in your profile_view, you can access these 2 variables from the session. You don't need to pass those 2 variables in the URL then.

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 中可能需要的会话中设置其他变量.

You can set other variables also in the session which you might need in profile_view.

这篇关于如何将变量从一个视图传递到另一个视图并在 Django 中使用最后一个视图的 URL 进行渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆