CSRF error

10 views
Skip to first unread message

kany...@fonality.com

unread,
Sep 9, 2015, 2:23:27 AM9/9/15
to Django users

Hi,

I have read through several post online about this CSRF issue and had no luck. I also read through the django documentation on CSRF and went step-by-step through the items needed to be added to the settings.py, template, etc for CSRF, and that was no luck as well. Here is what the webserver returned :

Reason given for failure:

    CSRF cookie not set.
    
The following are snippets of my files ...

1. Settings.py

MIDDLEWARE_CLASSES = (

    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.middleware.common.CommonMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',

    'django.contrib.auth.middleware.AuthenticationMiddleware',

    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

    'django.contrib.messages.middleware.MessageMiddleware',

    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'django.middleware.security.SecurityMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',

)

2. views.py:

def menu(request):

    return render_to_response('home/base_1.html', context_instance = RequestContext(request, {}) )

3. base.html:

          <form class="col-lg-12" method = "post" id="loginform" action= "/menu/">
            {% csrf_token %}

            <div class="input-group" style="width: 65%;0px;text-align:center;margin:0 auto;">
            <input class="form-control input-lg" title="Don't worry. We hate spam, and will not share your email with anyone." placeholder="Email address" type="text">             
            </div>
            <br>
            <div class="input-group" style="width: 65%;0px;text-align:center;margin:0 auto;">
              <input class="form-control input-lg" title="Don't worry. We hate spam, and will not share your email with anyone." placeholder="Password" type="text">
            </div>  
            <br>
            <br>      
           <button class="btn btn-lg btn-primary" style = "width: 100px" type="button" onClick ="logIn();">Log In</button>

4. login.js :

function logIn ()
{

    alert('Form has been submitted');
    document.getElementById('loginform').submit();

}

Reply all
Reply to author
Forward
0 new messages