def get_novel_list_common_data(request, novel_list): paginator = Paginator(novel_list, settings.EACH_PAGE_BLOGS_NUMBER) # 每页显示条数 page_num = request.GET.get('page', 1) # 获取url的页码参数(GET请求) page_of_novels = paginator.get_page(page_num) current_page_num = page_of_novels.number # 获取当前页码 # 获取当前页的前后两页 page_range = [ i for i in range(max(current_page_num - 2, 1), min(current_page_num + 2, paginator.num_pages) + 1) ] # 加上省略页码标记 if page_range[0] - 1 >= 2: page_range.insert(0, '...') if paginator.num_pages - page_range[-1] >= 2: page_range.append('...') # 加上首页和尾页 if page_range[0] != 1: page_range.insert(0, 1) if page_range[-1] != paginator.num_pages: page_range.append(paginator.num_pages) context = get_statistics_info(request) # 需要查找的所有小说 context['novel_list'] = page_of_novels.object_list # 分页显示的小说 context['page_of_novels'] = page_of_novels # 分页页码 context['page_range'] = page_range return context
def get_novel_type(request, group_id): book_list = Bookshelf.objects.all() group_list = BookshelfGroup.objects.all() group_book_list = Bookshelf.objects.filter(shelfgroup_id=group_id) context = get_statistics_info(request) context['book_list'] = book_list context['group_book_list'] = group_book_list context['group_list'] = group_list context['select'] = group_id return context
def login(request): if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): user = login_form.cleaned_data['user'] auth.login(request, user) return redirect(request.GET.get('from'), reverse('home')) else: login_form = LoginForm() context = get_statistics_info(request) context['login_form'] = login_form return render(request, 'user/login.html', context)
def register(request): if request.method == 'POST': reg_form = RegForm(request.POST, request=request) if reg_form.is_valid(): username = reg_form.cleaned_data['username'] email = reg_form.cleaned_data['email'] password = reg_form.cleaned_data['password'] user = User.objects.create_user(username, email, password) user.save() # 用户登录 user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(request.GET.get('from'), reverse('home')) else: reg_form = RegForm() context = get_statistics_info(request) context['reg_form'] = reg_form return render(request, 'user/register.html', context)
def change_password(request): redirect_to = reverse('home') if request.method == 'POST': user = request.user form = ChangePasswordForm(request.POST, user=user) if form.is_valid(): new_password = form.cleaned_data['new_password'] user.set_password(new_password) user.save() auth.logout(request) return redirect(redirect_to) else: form = ChangePasswordForm() context = get_statistics_info(request) context['page_title'] = '修改密码' context['form_title'] = '修改密码' context['submit_text'] = '修改' context['form'] = form context['return_back_url'] = redirect_to return render(request, 'form.html', context)
def change_nickname(request): redirect_to = request.GET.get('from', reverse('home')) if request.method == 'POST': user = request.user form = ChangeNicknameForm(request.POST, user=user) if form.is_valid(): nickname_new = form.cleaned_data['nickname_new'] user.nickname = nickname_new user.save() return redirect(redirect_to) else: form = ChangeNicknameForm() context = get_statistics_info(request) context['page_title'] = '修改昵称' context['form_title'] = '修改昵称' context['submit_text'] = '修改' context['form'] = form context['return_back_url'] = redirect_to return render(request, 'form.html', context)
def home(request): novels = Novel.objects.all()[:14] context = get_statistics_info(request) context['novels'] = novels return render(request, 'home.html', context)
def user_info(request): context = get_statistics_info(request) context['user'] = request.user return render(request, 'user/user_info.html', context)
def get_novel_type(request): novel_types = NovelType.objects.all() context = get_statistics_info(request) context['types'] = novel_types return context
def get_novel_and_novelType(request, novel_pk): novel = Novel.objects.filter(pk=novel_pk).first() context = get_statistics_info(request) context['novel'] = novel return context