def index(request): # マイページには自分の投稿記事とプロフィールを表示する book_records = Book.objects.order_by('create_date').reverse().all() # 本全体のアクセス数をカウントする books = [] for book in book_records: bc = BookComponent(book.id) if bc.draft == 1: books.append(bc.book_info()) # 検索フォームの作成 search_form = SearchForm() # 人気記事top10 popular_books = [] popular_records = Book.objects.order_by('good_count').reverse().all()[:10] for book in popular_records: bc = BookComponent(book.id) if bc.draft == 1: popular_books.append(bc.book_info()) data = { 'books': books, 'search_form': search_form, 'popular_books': popular_books } return custom_render(request, 'pyarticle/index.html', data)
def login(request): if request.method == 'POST': #ここのフォームがcreate用のフォームになっている。 form = AuthenticationForm(data=request.POST) if form.is_valid(): user = form.get_user() auth.login(request, user) return redirect('my_page') else: form = AuthenticationForm() return custom_render(request, 'pyarticle/account/login.html', {'form': form})
def signup(request): """ サインアップでユーザ新規追加という意味らしい :param request: :return: """ if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save() auth.login(request, user) return redirect('index') else: form = UserCreationForm() return custom_render(request, 'pyarticle/account/signup.html', {'form': form})
def search(request): """ 本を検索する :param request: :return: """ results = [] # 検索窓には、検索したときの文字を表示したい if request.method == 'POST': search_form = SearchForm(request.POST) if search_form.is_valid(): key_word = search_form.cleaned_data['key_word'] results = search_books(key_word) else: search_form = SearchForm() data = {'search_form': search_form, 'results': results} return custom_render(request, 'pyarticle/book_search.html', data)
def index_old(request): """ 登録されている本一覧をカテゴリごとに表示する :param request: :return:なし """ # 検索フォームの作成 search_form = SearchForm() # カテゴリの取得 categories = Category.objects.all().order_by('category_name') records = {} for category in categories: book_records = Book.objects.filter( category=category).order_by('id').reverse() if len(book_records) == 0: continue records[category.category_name] = [] # カテゴリごとに本をまとめる for book_record in book_records: bc = BookComponent(book_record.id) acc = bc.get_book_access_count() if len(book_record.description) > 10: book_record.description = book_record.description[0:100] first_chapter = Chapter.objects.filter( book=book_record).order_by('order').first() first_section = Section.objects.filter( chapter=first_chapter).order_by('order').first() records[category.category_name].append( [book_record, acc, first_chapter, first_section]) return custom_render(request, 'pyarticle/index.html', { 'book_records': records, 'search_form': search_form })