Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

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

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&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)

urls for this,

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'),

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.

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.

So the summary of the question,

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...