Python / Django多页形式重定向

时间:2014-06-15 00:35:18

标签: python django django-sessions

我目前有以下观点,两种控制形式。在通过重定向移动到第二个视图之前,选择广告系列会将发布的数据保存到会话变量。然而,问题出现了AddMessage的get方法被忽略并直接进入post方法,然而表单的后半部分由get方法输出。

有人对这种情况有所了解吗?

class SelectCampaign(View):
    template_name = "dash/SelectCampaign.html"

    form = messageForm1()


    def get(self, request, *args, **kwargs):
        user = User.objects.get(username=request.user.username)
        up = user.get_profile()
        self.form.fields['location'].queryset = up.owned_locations.all()
        return render(request, self.template_name, {'form': self.form})

    def post(self, request, *args, **kwargs):
        form = messageForm1(request.POST)
        if form.is_valid():
            request.session['MMANAGER_FORM_CTYPE'] = form.cleaned_data['campaign_type']
            request.session['MMANAGER_FORM_LOCATION'] = form.cleaned_data['location']
            return redirect(reverse('add_message'))
        else:
            #say whats not valid and return it
            form = messageForm1(request.POST)
            return render(request, self.template_name, {'form': form})



class AddMessage(View):

    template_name = "dash/AddMessage.html"
    form = messageForm2()

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, {'form': self.form})

    def post(self, request, *args, **kwargs):
        form = messageForm2(request.POST)
        if form.is_valid():
            campaign_type = request.session.get('MMANAGER_FORM_CTYPE')
            location = request.session.get('MMANAGER_FORM_LOCATION')
            ...
            return redirect(reverse('message_manager'))
        else:
            #say whats not valid and return it
            form = applicantsForm(request.POST)

            return render(request, self.template_name, {'form': form})

0 个答案:

没有答案