参照了潘军杰的博客,但他Django版本有点老了,改动了不少东西,总体来说参考价值还是很大的,点http://www.2goo.info/blog/panjj/Django/2010/06/05/94跳转
按上Django的可以去目录下找auth这个包,它涵盖了登录注册的大部分功能,我们拿过来直接用就可以了。我Python按在了D盘,所以目录就是这个,D:\Python2.7.6\Lib\site-packages\django\contrib\auth。不会用某个函数看一下这里面或者去看官方文档帮助都很大。最后我会提供工程压缩包,有兴趣的可以下载。
更新下载链接:http://download.csdn.net/detail/u011613729/7066341
1.先建立自己的项目,推荐大家使用eclipse+pydev+django的组合,比我之前一直用的cmd方便太多了!首先说一下我的目录结构,如图
MyLogin是创建项目的时候自动创建的,同级的还有manage.py。我自己创建了login文件夹存放登录的views和form,templates文件夹存放模板。tests、models那些都是自动生成的,这次并没有用到。
切记在settings.py里设置自己的模板目录!如果你下载压缩包一定要改这里,而且不能直接复制地址栏的目录,要把'\'全部换成‘/’!!!
2.也是从urls入手,其实只需要一个注册界面和一个登陆界面,所以代码如下
from django.conf.urls import patterns, include, url
from django.contrib import admin
from login.views import register,mylogin,changepassword
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^register/$',register),
(r'^login/$',mylogin),
(r'^login/register/$',register),
(r'^changepassword/(?P\w+)/$',changepassword),
)
3.接下来编写form表单,新建一个py文件命名为form。
注册的时候我们要求用户填用户名、邮箱、还要输入两次密码,所以我们的RegisterForm就诞生了。
from django import forms
class RegisterForm(forms.Form):
username = forms.CharField()
email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput)
password2= forms.CharField(label='Confirm',widget=forms.PasswordInput)
def pwd_validate(self,p1,p2):
return p1==p2
同样登录界面只需要用户名和密码即可,所以LoginForm更简单
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
from django.shortcuts import render_to_response
from form import RegisterForm,LoginForm,ChangepwdForm
from django.contrib.auth import authenticate,login as auth_login,logout as auth_logout
from django.http import HttpResponsefrom django.http import HttpResponseRedirect
def mylogin(request):
error = []
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
data = form.cleaned_data
username = data['username']
password = data['password']
if login_validate(request,username,password):
return render_to_response('welcome.html',{'user':username})
else:
error.append('Please input the correct password')
else:
error.append('Please input both username and password')
else:
form = LoginForm()
return render_to_response('login.html',{'error':error,'form':form})
def login_validate(request,username,password):
rtvalue = False
user = authenticate(username=username,password=password)
if user is not None:
if user.is_active:
auth_login(request,user)
return True
return rtvalue
Login
Login
{% for item in error %}
{{item}}
{% endfor %}
def register(request):
error=[]
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
data = form.cleaned_data
username = data['username']
email = data['email']
password = data['password']
password2= data['password2']
if not User.objects.all().filter(username=username):
if form.pwd_validate(password, password2):
user = User.objects.create_user(username, email, password)
user.save()
login_validate(request,username,password)
return render_to_response('welcome.html',{'user':username})
else:
error.append('Please input the same password')
else:
error.append('The username has existed,please change your username')
else:
form = RegisterForm()
return render_to_response('register.html',{'form':form,'error':error})
Join us
Register
{% if form.errors %}
Please correct the error{{ form.errors|pluralize }} below.
{% endif %}
{% for item in error %}
{{item}}
{% endfor %}
def mylogout(request):
auth_logout(request)
return HttpResponseRedirect('/login/')
def changepassword(request,username):
error = []
if request.method == 'POST':
form = ChangepwdForm(request.POST)
if form.is_valid():
data = form.cleaned_data
user = authenticate(username=username,password=data['old_pwd'])
if user is not None:
if data['new_pwd']==data['new_pwd2']:
newuser = User.objects.get(username__exact=username)
newuser.set_password(data['new_pwd'])
newuser.save()
return HttpResponseRedirect('/login/')
else:
error.append('Please input the same password')
else:
error.append('Please correct the old password')
else:
error.append('Please input the required domain')
else:
form = ChangepwdForm()
return render_to_response('changepassword.html',{'form':form,'error':error})
Welcome
Welcome {{user}}!
ChangePassword
ChangePassword
{% for item in error %}
{{item}}
{% endfor %}
有问题欢迎留言讨论交流,转载请注明出处:http://blog.csdn.net/monkeyduck