django动态选择域中的选定选项

时间:2009-12-21 11:37:25

标签: django

我在django

中创建了一个关键字过滤器

我的views.py

#..............
if request.method == 'POST':
                 form = FilterContentForm(request.POST)
        else:
                 form = FilterContentForm()
        if len(keyword_dict)!= 0 and keyword_dict['customer_type']:
            list_customer = filter(keyword_dict['customer_type'])
            print keyword_dict
            return render_to_response('customers_filter.html', {"customers":list_customer,
                                                                     "form":form
                                                                     })

我的forms.py

#..............
CUSTOMER_TYPE_CHOICES = [('', 'All')] + [(customer_type.name, customer_type.name) for customer_type in Customer_Type.objects.all()]

class FilterContentForm(forms.Form):
    customer_type = forms.ChoiceField(choices=CUSTOMER_TYPE_CHOICES, required=False)  
    def __init__(self, *args, **kwargs):
        if 'label_suffix' not in kwargs:
            kwargs['label_suffix'] = ''
        super(FilterContentForm, self).__init__(*args, **kwargs)

我将表单值填充到模板

{% extends "base.html" %}
{% block external %}
   <script type="text/javascript" src="/site_media/scripts/search.js"></script>
{% endblock %}

{% block content %}
{% block main %}
<form id="search-form" method="GET" action="." name="f">
{{ form.as_ul }}

<button id="filter">Filter</button>
</form>
<p>
<div id="search-results">
  {% if customers %}
   {% include 'customers.html' %}
  {% endif %}
</div>
{% endblock %}
{% endblock %}

例如有03个选项     -所有     -TDO     -STU 我点击了TDO,然后点击过滤按钮,之后就不记得我选择的选项(TDO)

单击过滤器后的URL

show/?customer_type=TDO

这里的任何人都可以找出我的问题。我有什么问题

1 个答案:

答案 0 :(得分:1)

HTML:

<form id="search-form" method="GET" action="." name="f">

视图:

if request.method == 'POST':

那么..也许你需要method =“POST”?