Héléna
Héléna

Reputation: 1095

django- How to filter the database based on the submitted form data

I have been stuck in this function for a while. Your help is very much appreciated.

The user enter the information in the form and submit it. Then based on user entry, we do a filter on the database and show in the template with 2 tables: one is the data entry form, another is the result after filtering.

Take a brief example:

In form, there are “age”,“name“ information. User fills it and submit the form. Then in database, we get the score based on the "age" and "name".

So in summary, there are two html shown as below:

enter image description here

They are in the same app. Please see below the snippets. My main questions are listed in the views.py.

models.py

class Input(models.Model):
    name=models.CharField(max_length=100)
    age=models.IntegerField(blank=False,null=False)

class Result(models.Model):
    score=models.IntegerField(blank=False,null=False)
    subjects=models.CharField(max_length=100)

forms.py

class InputForm(forms.ModelForm):
......

views.py

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

    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']

                # Here how can filter on the database based on form entry?

                queryset=queryset.filter(name=name,age=age)
                scor=Result.objects.filter(queryset)
                subject=Result.objects.filter(queryset)

                # how can I show the result in the redirected page?
                return redirect('result')

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

2nd solution of views.py

**views.py**

        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']

                    # how can I show the result in the redirected page?
                    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) to filter?                    
    queryset=queryset.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)
        context['input']=....?  /*how can I show the entry information as well here?

        return context

url

url(r'^result_list/$',ResultView.as_view(),name='result'),
url(r'^input', 'result.views.input',name='input'), /*then here 2 url refers to the same views..I know it is not correct, so should I put input and result in 2 different views. def? Then how can filter based on the form input?

html snippet for result page

<div class="informtaion">         
  <!--information from the entry page shown on the left part-->
    {% for input_object in input %}
        <table border="1" cellpadding="1">
        <tr>
            <td align="left">name</td>
            <td>{{input_object.name}}</td>
        </tr>
        <tr>
            <td align="left">age</td>
            <td>{{input_object.age}}</td>
        </tr>       
      </table>
   {% endfor %}
   </div>        

    <div class="">   <!--result shown on the right part-->
    {% for list in result_list %}

        <table border="0" cellspacing="10" cellpadding="10">        
        <td bgcolor="#F0F0F0"> {{list.subject}}</td>    
        <td bgcolor="#F0F0F0"> {{list.scor}}</td>
        .................       
    {% endfor %}
    </table>
    </div>

Upvotes: 1

Views: 2846

Answers (1)

kartikmaji
kartikmaji

Reputation: 966

In place of

return redirect('result')

Use

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

And to render in html.

<div class="">   <!--result shown on the right part-->
<table border="0" cellspacing="10" cellpadding="10">        
{% for list in score %}
    <td bgcolor="#F0F0F0"> {{list.subject}}</td>  
    <td bgcolor="#F0F0F0"> {{list.scor}}</td>
    .................       
{% endfor %}
</table>
</div>

Upvotes: 1

Related Questions