How do I pass variables from one view to another and render with the last view's URL in Django?

只愿长相守 提交于 2019-11-29 10:42:35

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

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
    ..

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
    ..

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

Do not think views, think code

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!