Demo entry 6357895

eee

   

Submitted by anonymous on Apr 24, 2017 at 07:39
Language: Python 3. Code size: 3.0 kB.

from django.shortcuts import render
from django.views.generic.edit import CreateView
from django.views.generic.detail import  DetailView
from django.views.generic.list import ListView
from app import models
from app import forms
from django.utils import timezone
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger





class ArticleDetailView(DetailView):
    model = models.Article
    template_name = 'article_detail.html'

    def get_context_data(self, **kwargs):
        context = super(ArticleDetailView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context



class FullSearchListView(ListView):
    model = models.Article
    template_name = 'full_search.html'
    paginate_by = 100

    def get_context_data(self, **kwargs):
        context = super(FullSearchListView, self).get_context_data(**kwargs)

        # 添加topic分类
        search_key = self.request.GET.get('search_key')
        print(search_key)
        if search_key:
            list_exam = models.Article.objects.filter(content__contains=search_key).order_by('-update_time').all()
            print(len(list_exam))
        else:
            list_exam = models.Article.objects.all().order_by('-update_time')
        context['list_article'] = list_exam
        return context







sorted([],)
"""
list all pages

"""

class ArticleListView(ListView):
    model = models.Article
    template_name = 'article_list.html'
    paginate_by = 100

    def get_context_data(self, **kwargs):
        context = super(ArticleListView, self).get_context_data(**kwargs)

        # 添加topic分类
        topic = self.request.GET.get('topic')
        # print(self.request.GET)
        # print(topic)
        if topic:
            list_exam = models.Article.objects.filter(topic=topic).order_by('-update_time')
        else:
            list_exam = models.Article.objects.all().order_by('-update_time')
        print(len(list_exam))
        paginator = Paginator(object_list=list_exam, per_page=self.paginate_by)
        page = self.request.GET.get('page')

        try:
            file_exams = paginator.page(page)
        except PageNotAnInteger:
            file_exams = paginator.page(1)
        except EmptyPage:
            file_exams = paginator.page(paginator.num_pages)

        topics = models.Topic.objects.all()
        context['topics'] = topics
        context['list_exams'] = file_exams
        print(context.keys())
        print(context.get('page_obj'))
        print(context.get('paginator'))
        return context


def me(request):
    '''
    个人简介
    :param request:
    :return:
    '''
    return render(request,'about.html')


def full_text_search(request):
    '''全文搜索'''
    pass
    #TODO

def toic_list_pages(request):
    '''根据topic 来显示文章'''
    pass


def check(req):
    return render(req,'check.html')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).