环境:数据库:mysql5.7communitymysql下载地址:https://dev.mysql.com/downloa...开发语言:Python3.7.364bit下载mysql驱动:https://www.lfd.uci.edu/~gohl...admin后台优化:django-xadmindjango安装:pipinstalldjango==2.2-ihttps://pypi.douban.com/simple镜像安装:https://pypi.douban.com/simple,示例:pip安装django==2.2-ihttps://pypi.douban.com/simplecaptcha插件:djangosimplecaptchahttps://django-simple-captcha...rediswindows:https://github.com/ServiceSta...Redispython驱动:https://github.com/andymccurd...分页插件:django纯分页https://github.com/jamespacil...技术要点:格式>>>"{}{}"。format("hello","world")#不设置指定位置,按照默认顺序'helloworld'>>>"{0}{1}".format("hello","world")#设置指定位置'helloworld'>>>"{1}{0}{1}".format("hello","world")#设置指定位置'worldhelloworld'http数据到前端fromdjango.httpimportHttpResponseRedirect,JsonResponsereturnJsonResponse({})FORM字段分离验证类RegisterPostForm(forms.Form):mobile=forms.CharField(required=True,min_length=11,max_length=11)code=forms.CharField(required=True,min_length=4,max_length=4)password=forms.CharField(required=True)#验证手机号是否已注册defclean_mobile(self):mobile=self.data.get("mobile")user=UserProfile.objects.filter(mobile=mobile)ifuser:raiseforms.ValidationError("Thisphonenumberhasalreadybeenregistered")returnmobile防止basemodel字段生成表classBaseModel(models.Model):add_time=models.DateTimeField(default=datetime.now,verbose_name="addtime")classMeta:#PreventBaseModelgeneratingtableabstract=Truedjangobuilt-inuserpasswordencryptionmethoduser=UserProfile(mobile=mobile)user.set_passdjangoword(password)#set_password生成加密密码HttpResponseRedirect页面重定向跳转ifuserisnotNone:#查询到用户login(request,user)#HttpResponseRedirect页面重定向returnHttpResponseRedirect(reverse("index"))模型foreignkeydefinitionclassLesson(BaseModel):#on_delete表示对应外键数据删除后对当前数据做什么#models.SET_NULL的值不级联删除,参数null=True,blank=True是必需的。值为模els.CASCADE将级联删除#course=models.ForeignKey(Course,on_delete=models.SET_NULL,null=True,blank=True)course=models.ForeignKey(Course,on_delete=models.CASCADE)name=models.CharField(verbose_name="章节名称",max_length=100)learn_times=models.IntegerField(verbose_name="学习时间(分钟)",默认=0)classMeta:verbose_name="coursechapter"verbose_name_plural=verbose_namedef__str__(self):returnself.namepaginationclassCourseListView(View):defget(self,request,*args,**kwargs):#获取课程列表all_courses=Course.objects.all().order_by("-add_time")#对课程数据进行分页尝试:page=request.GET.get('page',1)除了PageNotAnInteger:page=1p=Paginator(all_courses,per_page=5,request=request)courses=p.page(page)returnrender(request,"course-list.html",{"all_courses":courses})#注意all_courses.object_list{%forcourseinall_courses.object_list%}{%endfor%}
