Reputation: 187
I'm attempting to make a simple login framework for use later on in my site, at the moment its very basic, but I keep being stalled by the Django CSRF protection
Steps I have taken:
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',
)
Enabled the CSRFViewMiddleware
def request_page(request):
if(request.POST.get('btnLogin')):
if(handleLogin( (request.POST.get('TFUsername')), (request.POST.get('TFPassword')))):
return HttpResponse("yep")
else:
return HttpResponse("nope")
which calls this method in the login handler.py
def handleLogin(enteredUsername, enteredPassword):
EvalDBLogin.objects
b = EvalDBLogin(username = enteredUsername,password = enteredPassword)
if b.exists():
return True
else:
return False
The view function does pass a request to the template's render method as shown above
Every form element contains {% csrf_token %}
<td width="252"> </td>
<td width="272"><table width="258" height="115" border="0" align="center">
<tr>
<td width="248" height="27"><form id="form1" name="form1" method="post" action="#">{% csrf_token %}
<label for="TFUsername"></label>
<input name="TFUsername" type="text" class="loginBoxes" id="TFUsername" value="username" size="100" maxlength="42" border="5" width="200px"/>
</td>
</tr>
<tr>
<td height="26"><input name="TFPassword" type="password" class="loginBoxes" id="TFPassword" value="password" size="42" maxlength="42" /></td>
</tr>
<tr>
<td height="43">
<input type="submit" name="btnLogin" id="btnLogin" value="Submit" />
</form></td>
</tr>
</table></td>
<td width="252"> </td>
</tr>
</table></td>
Template Context Processors looks as follows:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"LoginSystem",
)
Am I missing something, or is it a technical problem my end?
Issue shown : http://puu.sh/jsRQh/384a552b2e.png
I know there are a few questions already on SO regarding this, but I couldn't quite find one that fully answered my question.
As a side note, the same error is returned if i exempt the method below from csrf
Help is greatly appreciated!
rendered Html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<style type="text/css">
.loginBoxes {
font-family: "Comic Sans MS", cursive;
font-size: 14px;
color: #AFAFAF;
background-color: #F8F8F8;
text-align: center;
width: auto;
}
#TFUsername {
border-radius: 15px 50px;
padding: 20px;
width: 300px;
height: 20px;
}
#TFPassword {
border-radius: 15px 50px;
padding: 20px;
width: 300px;
height: 20px;
}
#btnLogin{
border-radius: 25px;
background: #0066FF;
display: table-cell;
width: 310px;
display:table-cell;
margin:auto;
display:block;
height: 31px;
}
</style>
</head>
<body>
<table width="800" height="722" border="0" align="center">
<tr>
</tr>
<tr>
<td><table width="800" height="253" border="0" align="center">
<tr>
<td width="252"> </td>
<td width="272"><table width="258" height="115" border="0" align="center">
<tr>
<td width="248" height="27"><form id="form1" name="form1" method="post" action="#">
<label for="TFUsername"></label>
<input name="TFUsername" type="text" class="loginBoxes" id="TFUsername" value="username" size="100" maxlength="42" border="5" width="200px"/>
</td>
</tr>
<tr>
<td height="26"><input name="TFPassword" type="password" class="loginBoxes" id="TFPassword" value="password" size="42" maxlength="42" /></td>
</tr>
<tr>
<td height="43">
<input type="submit" name="btnLogin" id="btnLogin" value="Submit" />
</form></td>
</tr>
</table></td>
<td width="252"> </td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
Rendering of Template
def index(request):
template = loader.get_template('loginSystem/index.html')
return HttpResponse(template.render())
Upvotes: 1
Views: 430
Reputation: 309089
You have included the csrf middleware twice. You can remove it the second time.
'django.middleware.csrf.CsrfViewMiddleware',
You can remove the csrf_protect
decorator, since you are using the middleware, all views will be protected by default.
Since you are using RequestContext
, you can remove "django.core.context_processors.csrf"
from your template context processors, since it's always included.
The request context should be the third argument to render_to_response
, not the second:
return render_to_response('loginSystem/index1.html', {}, csrfContext)
But you'd be better to use the render
shortcut instead, then you don't need a request context at all.
from django.shortcuts import render
return render(request, 'loginSystem/index1.html')
If it still doesn't work after making those changes, please update your question above, and include what the post what the rendered template looks like.
Upvotes: 4