如何在Django中发送表单后再次显示以前选中的复选框?

时间:2013-10-11 10:39:50

标签: django django-templates

我正在使用Django为商店创建过滤器菜单。选中复选框后,用户会提交GET表单,并且站点会重定向到相同的URL,但会显示新产品结果。

过滤器显示已过滤的产品,但现在取消选中之前选中的复选框。我想保留那些选中的复选框。我怎么能这样做?

这是我的chocolate-menu.html:

<form action = '/chocolate-menu' method ='GET'  >
            <table width = "595px" class = "center_filter" style ="border-collapse:collapse;table-layout:fixed">
                <tr>
                    <td width = "110px" class = "no_border"> <span style = "color:9A8478;font-family: Helvetica Neue, Arial;font-weight: bolder; font-size:20px">Filter by</span> </td>
                    <td width = "119px"> <span style = "color:C39A6B;  font-weight: bold; font-size:18px">Price </span></td>
                    <td width = "119px"> <span style = "color:C39A6B;  font-weight: bold; font-size:18px">Flavour </span></td>
                    <td width = "128px"> <span style = "color:C39A6B;  font-weight: bold; font-size:18px">Special Diet </span></td>
                    <td width = "119px"> <span style = "color:C39A6B; font-weight: bold; font-size:18px">Calories </span></td>
                </tr>

                <tr>
                    <td class = "no_border"></td>
                    <td>  <input type="checkbox" style="margin-right: 10px;" id="five_to_ten" value='five_to_ten' name='five_to_ten' >&pound;5 - &pound;10</td>
                    <td> <input type="checkbox" style="margin-right: 10px;" id="dark" value = "dark" name = "dark">Dark </td>
                    <td> <input type="checkbox" style="margin-right: 10px;" id="lactose_free" value='lactose_free' name = "lactose-free">Lactose-free </td>
                    <td> <input type="checkbox" style="margin-right: 10px;" id="zero_to_hundret" value = 'zero_to_hundret' name='zero_to_hundret'>  0 - 100 </td>
                </tr></table></form>

这是views.py

中的巧克力菜单功能
def showChocoMenu(request):
    connection = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server=xx;Database=xx;Uid=xx;Pwd=xx;Encrypt=yes;Connection Timeout=30;')
    cur = connection.cursor()
    filter = "this filter gets a string that complements the SQL query later"
    if (filter == ''):
        cur.execute("SELECT distinct choco_name, choco_price FROM chocolates c, stock s WHERE c.choco_ID=s.choco_ID AND s.availability > 0 AND s.country ='UK'")
        chocolateMenu = cur.fetchall()
        cur.close()
        connection.close() 
    else:
        cur.execute("SELECT distinct choco_name, choco_price FROM chocolates c, stock s WHERE c.choco_ID=s.choco_ID AND s.availability > 0 AND s.country ='UK' AND " + filter )
        chocolateMenu = cur.fetchall()
        cur.close()
        connection.close() 
    return HttpResponse(render_to_string('chocolate_menu.html',{'chocolateMenu':chocolateMenu}))

1 个答案:

答案 0 :(得分:2)

您需要将GET参数传递回模板,如果存在,请将输入的checked属性设置为“已选中”:

# long-winded example for clarity
def showChocoMenu(request):
    ...
    five_to_ten = request.GET.get('five_to_ten')
    dark = request.GET.get('dark')
    lactose_free = request.GET.get('lactose-free')
    zero_to_hundret = request.GET.get('zero_to_hundret')

    return HttpResponse(render_to_string('chocolate_menu.html', 
        {'chocolateMenu':chocolateMenu, 'five_to_ten': five_to_ten,
        'dark': dark, 'lactose_free': lactose_free,
        'zero_to_hundret': zero_to_hundret}))

# shorter way
def showChocoMenu(request):
    ...
    context = {'chocolateMenu': chocolateMenu}
    for key in ['five_to_ten', 'dark', 'lactose_free', 'zero_to_hundret']:
        context.update({key: request.GET.get(key)})

    return HttpResponse(render_to_string('chocolate_menu.html', context))

# chocolate_menu.html
...
<tr>
    ...
    <td>
        <input type="checkbox" style="margin-right: 10px;" id="five_to_ten"
        value="five_to_ten" name="five_to_ten"
        {% if five_to_ten %}checked="checked"{% endif %} />
        &pound;5 - &pound;10
    </td>
    ...
</tr>