我正在尝试建立一个简单的网站,可以将数据添加到 MySQL 数据库中。我有一个带有两个文本输入(用户名、密码)的 POST 表单。我已阅读所有相关答案并试图解决它但未能成功。
索引.html
<form action="/login/" method="post">{% csrf_token %}<table border="0" cellspacing="15" width="345" align="center">
<tr>
<td width="100" >Username:</td>
<td><input type="text" class="text-box" value="{{ username }}" placeholder="Username"/></td>
</tr>
<tr>
<td class="align-left">Password:</td>
<td><input type="password" class="text-box" value="" placeholder="Password"/></td>
</tr>
<tr>
<td class="align-left"></td>
forget-link"><a href="#" >Forget Your Passowrd?</a></td>
</tr>
视图.py
def login(request):
logout(request)
username = password = ''
if request.method :
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/main/')
return render(request, 'index.html',{ 'username': username})
网址.py
urlpatterns = patterns('',
url(r'^login/', 'mysite.views.login', name='login'),
)
我在 index.html 中的表单标记后应用了 {% csrf_token %}。
当我单击登录按钮时,出现以下错误:
CSRF verification failed. Request aborted.