Search code examples
pythondjangodjango-formsdjango-csrf

CSRF verification error on GET form method


I am getting error CSRF verification failed. Request aborted. when trying to submit a form. I have used GET method, yet I am getting the error. I have added the {% csrf_token %} token also, just to be sure, but the error is still there.

Sample HTML:

<body>
    <form action="/entry/" method="get" name="Form1"><br>
        {% csrf_token %}
        <select name="Date" size="1">
            <option>30</option>
        </select>
        &nbsp;
        <select name="Month">
            <option>09</option>
        </select>
        <select name="Year">
            <option>2015</option>
        </select>
        <input id="Save" style="height: 50px; width: 100px;" type="submit" value="Save">
    </form>
</body>

views.py file

def getuser(request):
    return render(request, 'index.html')
    
def putrecord(request):
    date = request.GET['Date']
    print date
    month = request.GET['Month']
    year = request.GET['Year']
    time_stamp = date + '/' + month + '/' + year
    print time_stamp
    return render(request, 'index.html', {})
    

urls.py

urlpatterns = patterns('',

    url(r'^expenseapp/', getuser),
    url(r'^entry/', putrecord),
)

I also have CsrfViewMiddleware in my settings file. How can I get rid of this error?


Solution

  • Try to delete the cookies from your browser, CSRF use cookie to store the token.