有javascript表单和Django的问题

我已经查看并找到了类似于我的帖子,但我所拥有的一切似乎都处于正常工作状态,所以我很难过 .

我有一个javascript片段,允许用户选择两个建筑物中的一个,然后根据使用的建筑物给出不同的选项集 . 这是使用帖子提交按钮 . 到处都说它与{%csrf_token%}有关,但我已将其包含在表单中,并在settings.py中有中间件 . 如果它有所不同,那么同一页面上也会有一个get请求 .

表格

<form id="formname" name="formname" method="POST" action="">{% csrf_token %}
    <table width="50%" border="0" cellspacing="0" cellpadding="5">
        <tr>
            <td width="41%" align="right" valign="middle">Category :</td>
            <td width="59%" align="left" valign="middle">
                <select name="category" id="category" onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);">
                    <option value="">Select Building</option>
                    <option value="Marcus">Marcus</option>
                    <option value="Pettit">Pettit</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right" valign="middle">Location :</td>
            <td align="left" valign="middle">
                <script type="text/javascript" language="JavaScript">
                    document.write('<select name="subcategory"><option value="">Select Location</option>   </select>')
                </script>
                <noscript>
                    <select name="subcategory" id="subcategory">
                        <option value="">Select Location</option>
                    </select>
                </noscript>
            </td>
        </tr>
        <tr>
            <td>
                <td align="left" valign="middle">
                    <input value="Inventory Safety Log" type="submit">
                </td>
        </tr>
    </table>
</form>

Javascript

<script language="javascript" type="text/javascript">
function dropdownlist(listindex) {

    document.formname.subcategory.options.length = 0;
    switch (listindex) {

        case "Marcus":
            document.formname.subcategory.options[0] = new Option("Select Location", "");
            document.formname.subcategory.options[1] = new Option("Chemical Room", "Chemicals");
            document.formname.subcategory.options[2] = new Option("Supply Room", "Supplies");
            document.formname.subcategory.options[3] = new Option("Gas Storage G205", "G205");
            document.formname.subcategory.options[4] = new Option("In-Organic Sub Fab G230", "G230");
            document.formname.subcategory.options[5] = new Option("Gas Bunker", "Gas Bunker");
            break;
        case "Pettit":
            document.formname.subcategory.options[0] = new Option("Select Location", "");
            document.formname.subcategory.options[1] = new Option("Chemical Room", "Chemicals");
            document.formname.subcategory.options[2] = new Option("Supply Room", "Supplies");
            document.formname.subcategory.options[3] = new Option("Utility Chase #19", "19");
            document.formname.subcategory.options[4] = new Option("Utility Chase #25", "25");
            document.formname.subcategory.options[5] = new Option("Utility Chase #29", "29");
            document.formname.subcategory.options[6] = new Option("Utility Chase #30", "30");
            document.formname.subcategory.options[7] = new Option("Utility Chase #36", "36");
            document.formname.subcategory.options[8] = new Option("Utility Chase #44", "44");
            document.formname.subcategory.options[9] = new Option("Gas Cage", "Gas Cage");
            document.formname.subcategory.options[10] = new Option("Bunker", "Bunker");
            break;
    }
    return true;
}

</script>

感谢您提供任何意见,建议,提示或见解 .

主要问题是,当我尝试发布表单时,它会将我带到csrf验证失败页面 .

回答(1)

2 years ago

由于在运行中篡改表单数据,您遇到了问题 . 您需要使用django forms来干净地处理它 . 目前,您可以使用csrf_exempt装饰器作为临时解决方法