博客五部曲之二 - 高级博客
1409 浏览 5 years, 11 months
23 基本的用户登陆注册和注销
版权声明: 转载请注明出处 http://www.codingsoho.com/从现在开始我们来实现一些基本的注册登录功能等
我们创建一个新的应用accounts来完成这些功能
执行python manage.py startapp accounts生成新应用,记得把这个应用添加到INSTALLED_APP
创建登录视图处理函数、表单及模板,都是正常的操作,没什么特别
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
密码字段记得修改widget,这样密码输入就不会明文显示
from .forms import UserLoginForm
def login_view(request):
title = "login"
form = UserLoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
return render(request, "form.html", {"form":form, "title":title})
同样,在iblog.urls添加URL入口
from accounts.views import (login_view, register_view, logout_view)
urlpatterns = [
url(r'^login/', login_view),
]