提交HTML表单以更改django中的URL

时间:2014-10-24 07:34:03

标签: django html-form

我正在使用Django 1.6开发价格比较网站,现在我正在进行货币对话。现在我的网址格式如下:

http://www.example.com/product_search/123123123/currency

123123123是该产品的UID。当用户键入http://www.example.com/product_search/123123123/USD时,产品价格将更改为USDhttp://www.example.com/product_search/123123123/NZD会将货币更改为NZD。我可以通过更改URL来进行货币转换。

下一步是在我的html模板中创建一个表单,用户选择他/她的首选货币,点击提交,链接的货币参数将相应更改,并显示产品价格和首选货币。

我创建了我的html模板(部分内容),如下所示:

<form action='' class='sky-form' method='post'>
    <fieldset>
        <div class="row">
            <section class="col col-10">
                <label class="select">
                    <select name="Currency">
                        {% for currency_dict in currency_dict_qs %}
                            <option value="{{currency_dict.currency_code}}" {% with price_output_qs.all|first as price_output %}{% if price_output.product_currency_mod == currency_dict.currency_code %}selected{% endif %}{% endwith %}>
                                {{currency_dict.currency_name}} - {{currency_dict.currency_code}}
                            </option>
                        {% endfor %}
                    </select>
                    <i></i>
                </label>
            </section>
            <button type="submit" class="btn-u">Continue</button>
        </div>
    </fieldset>
</form>

我认为我非常缺乏知道Django如何与HTML表单交互的知识。我试过官方文件,但这对我来说似乎很复杂。通过举一些例子,任何人都可以激励我,以便我可以跟随并理解这些概念吗?

1 个答案:

答案 0 :(得分:0)

您正在自己构建表单,而不是使用Django中的Form API,您可以使用currency = request.POST.get('Currency', None)在视图中获取HTML选择值,然后将URL重定向到{{1 }}

在重定向到URL之前,不要忘记对从POST获得的值进行一些检查。