django 的登录验证

django 的登录验证
参考http://docs.djangoproject.com/en/1.2/topics/auth/
登录函数调用
from django.contrib.auth import authenticate, login

django 的登录验证
参考http://docs.djangoproject.com/en/1.2/topics/auth/
登录函数调用
[ccn lang="python" tab_size="4" theme="blackboard" width="800" ]
from django.contrib.auth import authenticate, login

def my_view(request):
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)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
[/ccn]
登出函数调用
[ccn lang="python" tab_size="4" theme="blackboard" width="800" ]
from django.contrib.auth import authenticate, login

def my_view(request):
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)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
[/ccn]
如果直接使用django的内部方法
只需要先配置url
在settings.py里添加
[ccn lang="python" tab_size="4" theme="blackboard" width="800" ]
(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'mark/login.html'}),
[/ccn]
在模板里需要username和password两个input提交POST方法到{% url django.contrib.auth.views.login %}
下面是一个例子
[ccn lang="python" tab_size="4" theme="blackboard" width="800" ]
{% extends "base.html" %}

{% block content %}

{% if form.errors %}

Your username and password didn't match. Please try again.

{% endif %}

{% csrf_token %}

{{ form.username.label_tag }} {{ form.username }}
{{ form.password.label_tag }} {{ form.password }}



{% endblock %}
[/ccn]