【问题标题】:How do I pass variables from one view to another and render with the last view's URL in Django?如何将变量从一个视图传递到另一个视图并在 Django 中使用最后一个视图的 URL 进行渲染?
【发布时间】: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_viewprocess_view 发起,则应呈现出勤率和分数的上下文。

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

所以问题的总结,

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

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

【问题讨论】:

    标签: python django django-views django-urls


    【解决方案1】:

    要访问profile_view中的tokenstudent_id变量,您可以使用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 中设置您可能需要的会话中的其他变量。

    【讨论】:

    • 这行得通,但请注意,这些值将保留在会话中,直到它们被重置......它们不会被重置,例如页面已加载
    • @gevra 是的,这些值将保留在会话中。如果需要,您可以在使用会话值后手动删除它。
    • 是的,只是觉得提一下很重要...request.session.pop('token', None) request.session.modified = True 会成功的
    • 嗨@gevra 我知道这有点晚了,但request.session.modified = True 到底是做什么的?
    • 来自documentation... "默认情况下,Django 仅在会话被修改时保存到会话数据库中——也就是说,如果它的任何字典值已被分配或删除"跨度>
    【解决方案2】:

    不要思考视图,思考代码

    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)
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2019-03-24
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多