samedi 29 novembre 2014

python - Django pass sélectionné objet de formulaire à une autre forme - Stack Overflow


I got 2 pages. Step 2 and Step 3. What I'm trying to do is pass the selected object from the step 2 form, to the step 3 form so I can filter the objects of the Step 3 forms. You can see the 2 pages/forms in the images below. So when a user selects a university in step 2, then the step 3 must show only the courses of the selected university.


My current code is really simple since I'm deleting and re-writing the code for the past days without results.


views.py


def step2(request):
universities = University.objects.order_by('name').distinct()
return render_to_response("registration/step2.html", {'universities': universities}, RequestContext(request))

def step3(request):
courses = Course.objects.order_by('name')
return render_to_response("registration/step3.html", {'courses': courses}, RequestContext(request))

Step 2 Step 3




In your view, you have to retrieve the selection that the user made and use it to filter the choices for the next form. Something like:


form = FirstForm(request.POST)
if form.is_valid():
uni = form.cleaned_data['uni']
courses = Course.objects.filter(university__name=uni).order_by('name')
return render_to_response("registration/step3.html", {'courses': courses}, RequestContext(request))


I got 2 pages. Step 2 and Step 3. What I'm trying to do is pass the selected object from the step 2 form, to the step 3 form so I can filter the objects of the Step 3 forms. You can see the 2 pages/forms in the images below. So when a user selects a university in step 2, then the step 3 must show only the courses of the selected university.


My current code is really simple since I'm deleting and re-writing the code for the past days without results.


views.py


def step2(request):
universities = University.objects.order_by('name').distinct()
return render_to_response("registration/step2.html", {'universities': universities}, RequestContext(request))

def step3(request):
courses = Course.objects.order_by('name')
return render_to_response("registration/step3.html", {'courses': courses}, RequestContext(request))

Step 2 Step 3



In your view, you have to retrieve the selection that the user made and use it to filter the choices for the next form. Something like:


form = FirstForm(request.POST)
if form.is_valid():
uni = form.cleaned_data['uni']
courses = Course.objects.filter(university__name=uni).order_by('name')
return render_to_response("registration/step3.html", {'courses': courses}, RequestContext(request))

0 commentaires:

Enregistrer un commentaire