2

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?

djvg
  • 11,722
  • 5
  • 72
  • 103
user828647
  • 505
  • 1
  • 10
  • 27

2 Answers2

1

The CSRF token template tag isn't required for forms submitted via GET. Simply removing the token from your form should allow it to reach your view handler method.

Similar question and answer: Django: POST form requires CSRF? GET doesn't?

Community
  • 1
  • 1
TeaPow
  • 687
  • 8
  • 17
  • Thanks for your reply. I had tried submitting without the csrf token as well, but it did not work. Anyway I deleted the cookies as well as the csrf token, and I finally got it to run. – user828647 Oct 04 '15 at 15:53
1

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

ahmed
  • 5,430
  • 1
  • 20
  • 36