Anthony Dellinger
Anthony Dellinger

Reputation: 107

Django only submitting csrf token in forms?

So, I have this in my template:

<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>

Which compiles to:

<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>

And when I click submit, firefox sends this parameter:

csrfmiddlewaretoken:"tGA4jKF1pd1QFC6NSAM9eNFvZqs60r4m"

That's it. No text, no color. I have no idea what's going on.

Upvotes: 3

Views: 1579

Answers (2)

veggie1
veggie1

Reputation: 747

As mentioned above, you need to specify a "name" attribute in your input fields. You don't need to use Django forms, but if you are submitting the form normally, any fields you are expecting to be sent need to have a name.

<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>

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599630

None of your fields have a name attribute, so the browser sends no data.

You should really be using Django's forms framework for this, though.

Upvotes: 6

Related Questions