django-如何根据提交的表单数据过滤数据库

时间:2015-10-20 06:09:41

标签: python django database forms

我已经被困在这个功能中一段时间​​了。非常感谢您的帮助。

用户在表单中输入信息并提交。然后根据用户输入,我们对数据库进行过滤,并在模板中显示2个表:一个是数据输入表,另一个是过滤后的结果。

举一个简短的例子:

在表格中,有“年龄”,“姓名”信息。用户填写并提交表格。 然后在数据库中,我们根据“年龄”和“名称”得到分数。

总而言之,有两个html显示如下:

enter image description here

他们在同一个应用程序中。请参阅下面的代码段。我的主要问题列在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))

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(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代码段

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

1 个答案:

答案 0 :(得分:1)

取代

UITableViewCell

使用

return redirect('result')

以html呈现。

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