def post(self,request): context = get_top_data(request) register_form = RegisterForm(request.POST) if register_form.is_valid(): username = request.POST.get('username','') user = UserProfile.objects.filter(username=username).all() if user: return render(request, 'register.html', {'msg': '用户 {0} 已存在'.format(username),'register_form':register_form}) email = request.POST.get('email','') password = request.POST.get('password','') surepassword = request.POST.get('surepassword','') if password != surepassword: context.update({'msg': '两次密码输入不一致','register_form':register_form}) return render(request, 'register.html', context=context) user_profile = UserProfile() user_profile.username = username user_profile.email = email user_profile.password = make_password(password) user_profile.is_active = False user_profile.save() send_register_email(email,username,'register') context.update({'email_msg': '注册邮件已发送,请注意查收.', 'register_form': register_form}) return render(request, 'register.html', context=context) context.update({'register_form':register_form}) return render(request, 'register.html',context=context)
def post(self,request): login_form = LoginForm(request.POST) context = get_top_data(request) if login_form.is_valid(): user_name = request.POST.get('username','') password = request.POST.get('password','') user = authenticate(username=user_name,password=password) if user is not None: login(request,user) return HttpResponseRedirect(reverse("home")) else: context.update({"msg": '用户名或密码错误,登录失败'}) return render(request, 'login.html', context=context) context.update({"login_form":login_form}) return render(request, 'login.html',context=context)
def get(self,request): username = request.GET.get("username","") code = request.GET.get("code","") context = get_top_data(request) if username and code: username = urllib.parse.unquote(username) obj = EmailVerifyRecord.objects.filter(username=username).filter(code=code).first() if obj: user = UserProfile.objects.filter(email=obj.email).filter(username=obj.username).first() user.is_active = True user.save() context['username'] = username context['email'] = user.email context['msg'] = '激活成功!!!' return render(request,'email_active.html',context) context['username'] = username context['msg'] = '激活失败!!!' return render(request, 'email_active.html', context=context)
def QuertBaseData(request): ''' 查询博客基本信息 :return: ''' context = get_top_data(request) new_article_list = Article.objects.order_by('create_time')[0:5] tags = Tag.objects.order_by('id').all() _date = Article.objects.annotate( year=ExtractYear('create_time'), month=ExtractMonth('create_time')).values( 'year', 'month').annotate(nums=Count('create_time')) group = Article.objects.all().values( 'category', 'category__name').annotate(total=Count('category')).order_by('total') read_num_article_list = Article.objects.all().order_by('-read_num')[0:5] new_comments = Comment.objects.all().order_by("-pub")[0:5] context['tags'] = { tag.id: [ tag.name, settings.TAG_COLOR_LIST[index % len(settings.TAG_COLOR_LIST)] ] for index, tag in enumerate(tags) } context['new_article_list'] = { article.id: article.title for article in new_article_list } context['article_types'] = { category['category']: category['category__name'] for category in group } context['date'] = [(d['year'], d['month'], d['nums']) for d in _date] context['group'] = [(category['category'], category['category__name'], category['total']) for category in group] context['read_num'] = [(d.id, d.title, d.read_num) for d in read_num_article_list] context['new_comment'] = [ (i + 1, new_comment) for i, new_comment in enumerate(new_comments) ] #[(i+1,new_comment.user,new_comment.blog.id,new_comment.blog.title,new_comment.content,new_comment.pub) for i,new_comment in enumerate(new_comments)] return context
def get(self,request): context = get_top_data(request) return render(request,'login.html',context=context)
def get(self,request): register_form = RegisterForm() context = get_top_data(request) context.update({'register_form': register_form}) return render(request, 'register.html', context=context)
def page_not_found(request): context = get_top_data(request) return render(request, '404.html', context=context)