当前位置: 首页 > 后端技术 > Python

Django20200424博客开发017

时间:2023-03-25 23:28:03 Python

使用django自定义模板标签使用DjangoForm实现功能包括如下图片:%}{%blocktitle%}我的网站|登录{%endblock%}{%blocknav_home_active%}active{%endblock%}{%blockcontent%}登录

{%csrf_token%}{%forfirein_FORM%}{{field.label}}{{字段}}{{field.errors.as_text}}

{%endfor%}{{login_form.non_field_errors}}
{%endblock%}重写mysite/views.py中的login数(反向解析获取获取主页链接)from.formsimportLoginForm,RegForm...deflogin(request):ifrequest.method=='POST':login_form=LoginForm(request.POST)iflogin_form.is_valid():user=login_form.cleaned_data['user']auth.login(request,user)返回重定向(request.GET.get('from',reverse('home')))其他:login_form=LoginForm()context={}context['login_form']=login_formreturnrender(request,'login.html',context)在blog_detail.html中登录前添加:你没有登录...你没有登录,登录后才能评论~loginregister然后使用DjangoForm:在mysite中创建forms.py文件,然后创建loginFormfunctionfromdjangoimportformsfromdjango.contribimportauthfromdjango.contrib.auth.modelsimportUserclassLoginForm(forms.Form):用户名=forms.CharField(label='username',widget=forms.TextInput(attrs={'class':'form-control','placeholder':'请输入您的用户名'}))password=forms.CharField(label='password',widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'请输入您的密码'}))defclean(self):username=self.cleaned_data['username']password=self.cleaned_data['密码']user=auth.authenticate(username=username,password=password)ifuserisNone:raiseforms.ValidationError('Usernameorpasswordisincorrect')else:self.cleaned_data['user']=user.lfreturned_data添加注册函数:在forms.py中添加RegForm类:#forms.pyclassRegForm(forms.Form):"""用户注册功能实现"""username=forms.CharField(label='username',max_length=30,min_length=3,widget=forms.TextInput(attrs={'class':'form-control','placeholder':'请输入3-30个用户名(Field.msformail=email=email=mail'}))'Email',Widget=forms.EmailInput(attrs={'class':'form-control','placeholder':'Pleaseentertheemail'}))password=forms.CharField(label='password',thleng_ng_,Widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'请输入密码'}))password_again=forms.CharField(label='再次输入密码',min_length=6,widget=forms.passwordinput(attrs={'class':'form-control','placeholder':':anotherpassword'}))deflean_username:"""Validateagainusername"""username=self.cleaned_data['username']ifUser.objects.filter(username=username).exists():raiseforms.ValidationError('usernamealreadyexists')urnreusernamedefclean_email(self):"""Validateforuseremail"""email=self.cleaned_data['email']ifUser.objects.filter(email=email).exists():ValidationorrrmsMailboxalreadyexists')returnemaildefclean_password_again(self):"""验证再次输入的密码是否与上面第一次输入的密码一致"""password=self.cleaned_data['password']andatainpassword_ag['password_again']ifpassword!=password_again:raiseforms.ValidationError('两次输入的密码不一致')returnpassword_again在mysite/views.py中添加了一个注册功能的处理函数,和登录功能基本一样"""RegisterFunctional视图处理函数"""ifrequest.method=='POST':reg_form=RegForm(request.POST)ifreg_form.is_valid():"""因为前端需要这些参数,所以这里我们首先从后端提取它们,然后返回给给“”“”用户名=reg_form.cleaned_data['username']email=reg_form.cleaned_data['email'emagy'password=reg_form.form.cleaned_data['密码']user=User.objects.create_user(username,email,password)user.save()#登录用户user=auth.authenticate(username=username,password=password)auth.login(verquest,user)fromreturnGET(request('request.se'home')))else:reg_form=RegForm()context={}context['reg_form']=reg_formreturnrender(请求,'注册。html',context)在总mysite/urls.py中添加registerroutematchingpath('register/',views.register,name='register'),在publictemplates中新建register.html,内容和登录同上.html,不同的是login改为registration,然后加几个标签:{%extends'base.html'%}{%loadstatic%}{%blocktitle%}我的网站|注册{%endblock%}{%blocknav_home_active%}active{%endblock%}{%blockcontent%}注册{%csrf_token%}{%forfieldinreg_form%}{{field.标签}}}};{{field}}{{field.errors.as_text}}

{%endfor%}{%endfor%}non_field_errors}}}{%endblock%}成果展示: