Héléna
Héléna

Reputation: 1095

django- how two def in the same views.py pass the data

In the same views.py, if I have 2 "def", how can I pass the data entry result in the 1st def to the 2nd def for filtering database?

Take an example:

def input(request):
    if request.method == 'POST':
        form = InputForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            age = form.cleaned_data['age']

            return redirect('result')
       else:
            print form.errors
   else:
       form=InputForm()             
   return render_to_response('inputform.html',{'form': form},context_instance=RequestContext(request))


class ResultView():
    context_object_name = 'result_list'
    template_name = 'result_list.html'

    # Here how can I get the form entry (name/age)from above def to filter the result?                    
    queryset=Result.filter(name=name,age=age)
    scor=Result.objects.filter(queryset)
    subject.Result.objects.filter(queryset)

    def get_context_data(self, **kwargs):
        context = super(ResultView, self).get_context_data(**kwargs)

        return context

url

url(r'^result_list/$',ResultView.as_view(),name='result'),
url(r'^input', 'result.views.input',name='input'),

In the above code, the queryset=Result.filter(name=name,...),here it cannot get the form data from the "def input". Your help is much appreciated, thanks.

Upvotes: 0

Views: 62

Answers (1)

EchoGlobal.tech
EchoGlobal.tech

Reputation: 704

class ResultView(ListView):
    context_object_name = 'result_list'
    template_name = 'result_list.html'

    def get_context_data(self, **kwargs):
        context = super(ResultView, self).get_context_data(**kwargs)
        return context

    def get_queryset(self):
        if self.request.method == 'POST':
            form = InputForm(self.request.POST)
            if form.is_valid():
                name = form.cleaned_data['name']
                age = form.cleaned_data['age']
                return Result.filter(name=name, age=age)
        return super(ResultView, self).get_queryset()

Upvotes: 1

Related Questions