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

前端 未结 2 1594
青春惊慌失措
青春惊慌失措 2020-12-10 08:22

I\'m building a student management system using Django.

In this code, The user search for a student with the encrypted query name=StudentName&grade=Grade&

相关标签:
2条回答
  • 2020-12-10 08:30

    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.

    0 讨论(0)
  • 2020-12-10 08:39

    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)
    
    0 讨论(0)
提交回复
热议问题