How to pass variables from one view to another and make with the last view URL in Django?

I am creating a student management system using Django.

In this code, the user is looking for a student with an encrypted request name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber which is retrieved using the decrypt() method.

Here are two methods that process the request and one that shows the student profile.

No data from the request is stored in the database, but will be used to request student information 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 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 class should be specified. If profile_view triggered by process_view , a context with attendance and labels should be displayed.

This works before redirecting process_view , but I have no clue where to reinstall (or even redirect) and call profile_view .

So, a summary of the question,

How to redirect from process_view to profile_view without losing data collected in process_view to profile_view and display content with the profile_view ? I do not want token and student_id be shown on the URL.

Thanks for any suggestions / help.

+4
source share
2 answers

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

In 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 two variables from the session. You do not need to pass these 2 variables to the 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 .. 

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

+6
source

Do not think about 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) 
+1
source

All Articles