Django只在表单中提交csrf令牌?

时间:2015-08-21 23:03:45

标签: django

所以,我在我的模板中有这个:

<form action="./validate_code/" method="POST">
    {% if error %}
        <p>Sorry, that wasn't right.</p>
    {% endif %}        
    <label for="class_code">Your class code: </label>
    <input type='text' id='class_code'/>    
    <input type="color" id="color"/>    
    {% csrf_token %}    
    <input type="submit" value="submit">
</form>

编译为:

<form action="./validate_code/" method="POST">
    <label for="class_code">Your class code: </label>
    <input type='text' id='class_code'/>    
    <input type="color" id="color"/>    
    <input type='hidden' name='csrfmiddlewaretoken' value='tGA4jKF1pd1QFC6NSAM9eNFvZqss0r4m' />    
    <input type="submit" value="submit">
</form>

当我点击提交时,firefox发送此参数:

csrfmiddlewaretoken:"tGA4jKF1pd1QFC6NSAM9eNFvZqs60r4m"

那就是它。没有文字,没有颜色。我不知道发生了什么。

2 个答案:

答案 0 :(得分:1)

您的所有字段都没有name属性,因此浏览器不会发送任何数据。

尽管如此,你应该真正使用Django的表单框架。

答案 1 :(得分:1)

如上所述,您需要在输入字段中指定“name”属性。您不需要使用Django表单,但如果您正常提交表单,那么您希望发送的任何字段都需要有一个名称。

<form action="./validate_code/" method="POST">
    {% if error %}
        <p>Sorry, that wasn't right.</p>
    {% endif %}
    <label for="class_code">Your class code: </label>
    <input type="text" name="class_code" id="class_code">
    <input type="color" name="color">
    {% csrf_token %}
    <button type="submit">Submit</button>
</form>
相关问题