在视图django之间传递值

时间:2013-03-20 11:35:35

标签: django django-forms django-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))

我想将post_type传递给第二个表单。在此基础上我想显示其他表单。谁能告诉我怎么能这样做?

1 个答案:

答案 0 :(得分: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))