Michael Smith
Michael Smith

Reputation: 3447

Sending parameters to view when submitting a form in Django

I need to send some parameters when submitting a form in Django and I'm not sure how to do this.

Right now I have a form inside a page(Page A) allows filtering-- so my url for that page looks like this example.com/view-students?grade=5&teacher=smith

Inside that page I have a pop-up that allows a user to delete a student. When the user confirms that they want to delete a student, the form is submitted-- this directs to another view that takes care of the deleting. Then, I immediately redirect back to Page A.

The problem is that I need the parameters (?grade=5&teacher=smith) back in my url to redirect correctly. How do I go about doing this?

Upvotes: 0

Views: 62

Answers (1)

Rahul Gupta
Rahul Gupta

Reputation: 47906

Since you need to pass variables from one view to another, you can set the value of grade and teacher in the session and then use this values in your second view to redirect it properly.

request.session['grade'] = 5
request.session['teacher'] = 'smith'

In your 2nd view, you can just do the following to access the value of grade and teacher:

grade = request.session['grade']
teacher = request.session['teacher']

Upvotes: 2

Related Questions