1

I want to pass value from one form to another but i am not using form wizard. my views:

def main_page(request):
    #if request.method == 'POST':
    #form = jobpostForm_first()
   # if request.method == 'POST':
    if request.method == 'POST':
       #if '_Submit'in request.POST:
            #if (form.data['post_type']=='Job'):
            form = jobpostForm_first(request.POST)
            if (form.data['post_type']=='Job'):

               #if form.is_valid():
                    #form.save()
                    return render_to_response('portal/job_post.html',{'form':form},context_instance=RequestContext(request))
            #else:
             #   return HttpResponseRedirect('/accounts/login/')

    else:
            form = jobpostForm_first()

    c = {}
    c.update(csrf(request))



    return render_to_response('portal/job_post.html',{'form':form},context_instance=RequestContext(request))


def next(request):
    #post_type= request.GET.get('post_type')

    if request.POST['post_type']=='Job': # here it is giving me exception "Key 'post_type' not found in <QueryDict: {}>"
       if request.method == 'POST':
            form = jobpostForm(request.POST)
            if form.is_valid():
                    form.save()

                    return HttpResponseRedirect('/thanks/')
       else:
            form = jobpostForm()
    else:
       return HttpResponseRedirect('/accounts/login/')

    c = {}
    c.update(csrf(request))

    return render_to_response('portal/job_post.html',{'form':form},context_instance=RequestContext(request))

I want pass the post_type to 2nd form.and on its basis i want to show other form. Can anyone tell me how can i do this?

8
  • 1
    This is exactly what form wizards are designed to do. They were created because it is not easy to get right. Use the form wizard, or specify why you can not. Commented Mar 20, 2013 at 11:46
  • the error shows that your POST request does not have any data, How are you passing the data ? Commented Mar 20, 2013 at 11:50
  • can u plz tell me how to pass the data? Commented Mar 20, 2013 at 11:56
  • if you want the post_type data as POSTdata simply add a hidden input like <input type="hidden" name="post_type" value="post_type_value_here" /> in the form or if you pass it as GET data append it to the url like 127.0.0.1:8000/?post_type=post_type_value or alternatively use session variables as @catherine suggested. Commented Mar 20, 2013 at 12:07
  • Amyth- I am doing this but still it gives me error "Key 'post_type' not found in <QueryDict: {}>" Commented Mar 20, 2013 at 12:22

1 Answer 1

3
def main_page(request):
    form = jobpostForm_first()

    if request.method == 'POST':
        form = jobpostForm_first(request.POST)
        if form.is_valid():
            post_type = form.cleaned_data['post_type']
            if post_type == 'Job':
                request.session['post_type'] = post_type
                return HttpResponseRedirect('/next/') //---> Is this the correct url for next?

    c = {}
    c.update(csrf(request))

    return render_to_response('portal/job_post.html',{
        'form':form
    },context_instance=RequestContext(request))


def next(request):
    if request.session.get('post_type') == 'Job': 
       if request.method == 'POST':
            form = jobpostForm(request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect('/thanks/')
       else:
            form = jobpostForm()
    else:
       return HttpResponseRedirect('/accounts/login/')

    c = {}
    c.update(csrf(request))

    return render_to_response('portal/job_post.html',{
        'form':form
    },context_instance=RequestContext(request))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanku so much for posting the answer but still it is not working..it always go to the '/accounts/login/' url means it is not getting value of post_type
That's the way on passing the value. I think you need to modify you next view. Even though the session variable pass it cannot continue because of that request.POST which you need to call it manually. That's why you always end up in /account/login/
ok n if i use formwizards for two forms then how can put condition of job_post on 2nd form?
You call the second view from the first one. This is a normal request, and not a POST request, so you always end up at /accounts/login/ . You should start your next view with: if request.method == 'POST': In your main_page view you should store the form values in a session, and access these values from your next view. You can populate your form, using initial. As Josh stated above, formwizard is designed to do this automatically for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.