1. 在 account app 下面继续实现,因此 不需要添加 app 也不用配置 settings.py,并且延用 account 的 user model 2.在 views.py 上添加 login 函数 class LoginForm(forms.Form): username = forms.CharField(label='用户名:', max_length=100) password = forms.CharField(label='密码:', widget=forms.PasswordInput()) def login(request): if request.method == "POST": uf = LoginForm(request.POST) if uf.is_valid(): username = uf.cleaned_data['username'] password = uf.cleaned_data['password'] user = User.objects.filter(username__exact = username, password__exact = password) if user: return render_to_response('success.html',{'username':username}) else: return HttpResponseRedirect('/login/') else: uf = LoginForm() return render_to_response('login.html',{'uf':uf}) 3.创建 login.html <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>登录</title> </head> <style type="text/css"> body{color:#efd;background:#453;padding:0 5em;margin:0} h1{padding:2em 1em;background:#675} h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em} p{margin:1em 0} </style> <body> <h1>登录页面:</h1> <form method = 'post' enctype="multipart/form-data"> {{uf.as_p}} <input type="submit" value = "ok" /> </form> </body> </html> 4./account/urls.py 配置 url from django.conf.urls import url from account import views as account_views urlpatterns = [ url(r'^$', account_views.register, name='register'), url(r'^register/$', account_views.register, name='register'), url(r'^login/$', account_views.login, name='login'), ] 5.success.html 里面创建一个 跳转到 login 的函数 <a href="{%url 'login'%}"> 去登陆 </a>
django 快速登录
最新推荐文章于 2018-09-03 09:48:01 发布