简体   繁体   中英

Django pass selected object from form to another form

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

第2步第三步

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM