如何将值从下拉列表传递到django中的视图

时间:2018-02-22 12:14:47

标签: python html django

我已经广泛搜索了我的问题的明确答案但是却找不到任何东西。我目前正在显示数据库中的前5个项目,并希望添加一个功能,允许用户使用当前租金值在升序和降序之间切换,但是我无法将下拉列表的值传递给视图并改变表的顺序。

HTML文件:

{% block content %}
<form method="GET" action="" >
    {% csrf_token %}
    {{ form.as_p }}
<select name="dropdown">
  <option value="dsc">Descending</option>
  <option value="asc">Ascending</option>
</select>
    <input type="submit" value="order">
</form>
<table id="myTable" class="tablesorter">
    <thead>
                <th>Property Name</th>
                <th>Property Address 1</th>
                <th>Property Address 2</th>
                <th>Property Address 3</th>
                <th>Property Address 4</th>
                <th>Unit Name</th>
                <th>Tenant Name</th>
                <th>Lease Start</th>
                <th>Lease End</th>
                <th>Lease Years</th>
                <th>Rent</th>
            </thead>
    {% for data in list.all %}
            <tr>
                <td>{{data.property_name}}</td>
                <td>{{data.property_address1}}</td>
                <td>{{data.property_address2}}</td>
                <td>{{data.property_address3}}</td>
                <td>{{data.property_address4}}</td>
                <td>{{data.unit_name}}</td>
                <td>{{data.tenant_name}}</td>
                <td>{{data.lease_start}}</td>
                <td>{{data.lease_end}}</td>
                <td>{{data.lease_years}}</td>
                <td>{{data.current_rent}}</td>
            </tr>

    {% endfor %}
</table>

{% endblock %}

Views.py

from django.db.models import Sum
from . import models
from .models import Stats
from django.views.generic import ListView,TemplateView,CreateView
# Create your views here.

class HomeView(TemplateView):
    template_name = 'index.html'

class StatsListView(ListView):
    context_object_name = 'list'
    model = models.Stats

    def get_queryset(self):

        first_five = Stats.objects.all().order_by('current_rent')[:5]
        return first_five

    def PageObjects(self,request):
        answer = request.GET['dropdown']
        if request.method == 'POST':
            if answer == 'dsc':
                Stats.objects.all().order_by('-current_rent')[:5]
            else:
                Stats.objects.all().order_by('current_rent')[:5]

1 个答案:

答案 0 :(得分:0)

我建议您发送数据:

{% block content %}
<form method="Post" action="" >
    {% csrf_token %}
    {{ form.as_p }}
<select name="dropdown">
  <option value="dsc">Descending</option>
  <option value="asc">Ascending</option>
</select>
    <input type="submit" value="Select">
</form>
<table id="myTable" class="tablesorter">
    <thead>
                <th>Property Name</th>
                <th>Property Address 1</th>
                <th>Property Address 2</th>
                <th>Property Address 3</th>
                <th>Property Address 4</th>
                <th>Unit Name</th>
                <th>Tenant Name</th>
                <th>Lease Start</th>
                <th>Lease End</th>
                <th>Lease Years</th>
                <th>Rent</th>
            </thead>
    {% for data in list.all %}
            <tr>
                <td>{{data.property_name}}</td>
                <td>{{data.property_address1}}</td>
                <td>{{data.property_address2}}</td>
                <td>{{data.property_address3}}</td>
                <td>{{data.property_address4}}</td>
                <td>{{data.unit_name}}</td>
                <td>{{data.tenant_name}}</td>
                <td>{{data.lease_start}}</td>
                <td>{{data.lease_end}}</td>
                <td>{{data.lease_years}}</td>
                <td>{{data.current_rent}}</td>
            </tr>

    {% endfor %}
</table>

{% endblock %}

Views.py

from django.db.models import Sum
from . import models
from .models import Stats
from django.views.generic import ListView,TemplateView,CreateView
# Create your views here.

class HomeView(TemplateView):
    template_name = 'index.html'

class StatsListView(ListView):
    context_object_name = 'list'
    model = models.Stats

    def get_queryset(self):

        first_five = Stats.objects.all().order_by('current_rent')[:5]
        return first_five

    def PageObjects(self,request):
       Changes Begin Here =========================>
        if request.method == 'POST':
            form = YourForm(request.POST)
            if form.is_valid():
                answer = form.cleaned_data['value']
       Changes End Here ===========================>
            if answer == 'dsc':
                Stats.objects.all().order_by('-current_rent')[:5]
            else:
                Stats.objects.all().order_by('current_rent')[:5]