Beispiel #1
0
def index(request):
    ct = ContentType.objects.get_for_model(Blog)
    dates, read_nums = get_seven_days_read_date(ct)
    today_hot_blogs = get_today_hot_date(ct)
    yesterday_hot_blogs = get_yesterday_hot_date(ct)
    seven_hot_blogs = get_seven_days_hot_date(ct)
    content = {
        'dates': dates,
        'read_nums': read_nums,
        'today_hot_blogs': today_hot_blogs,
        'yesterday_hot_blogs': yesterday_hot_blogs,
        'seven_hot_blogs': seven_hot_blogs
    }
    return render(request, 'index.html', content)
Beispiel #2
0
def home(request):
    blog_content_type = ContentType.objects.get_for_model(Blog)
    dates, read_nums = get_seven_days_read_date(blog_content_type)
    # 获取7天热门博客缓存数据
    hot_blogs_for_7_days = cache.get('hot_blogs_for_7_days')
    if hot_blogs_for_7_days is None:
        hot_blogs_for_7_days = get_7_days_hot_blogs()
        cache.set('hot_blogs_for_7_days', hot_blogs_for_7_days,
                  3600)  # 键,值,缓存时间(秒)
    context = dict()
    context['read_nums'] = read_nums
    context['dates'] = dates
    context['today_hot_data'] = get_today_hot_date(blog_content_type)
    context['yesterday_hot_data'] = get_yesterday_hot_data(blog_content_type)
    context['hot_data_for_7_days'] = get_7_days_hot_blogs()
    context['hot_data_for_30_days'] = get_30_days_hot_blogs()
    return render(request, 'home.html', context)
Beispiel #3
0
def home(request):
    blog_content_type = ContentType.objects.get_for_model(Blog)
    dates, read_nums = get_seven_days_read_data(blog_content_type)

    # 获取7天热门数据
    sevendays_hot_date = cache.get('sevendays_hot_date')
    if sevendays_hot_date is None:
        sevendays_hot_date = get_sevendays_hot_blogs()
        cache.set('sevendays_hot_date', sevendays_hot_date, 60*60)

    context = {}
    context['dates'] = dates
    context['read_nums'] = read_nums
    context['today_hot_date'] = get_today_hot_date(blog_content_type)
    context['yesterday_hot_date'] = get_yesterday_hot_date(blog_content_type)
    context['sevendays_hot_date'] = get_sevendays_hot_blogs()
    return render(request, 'home.html', context)
Beispiel #4
0
def home(request):
    blog_content_type = ContentType.objects.get_for_model(Blog)
    dates, read_nums = get_seven_days_read_date(blog_content_type)

    # 获取七天热门博客的缓存数据
    hot_blogs_for_7_days = cache.get('hot_blogs_for_7_days')
    if hot_blogs_for_7_days is None:
        hot_blogs_for_7_days = get_7_days_hot_blogs()
        cache.set('hot_blogs_for_7_days', hot_blogs_for_7_days, 3600)
        print('cals')
    else:
        print('use cache')

    context = {}
    context['dates'] = dates
    context['read_nums'] = read_nums
    context['today_hot_date'] = get_today_hot_date(blog_content_type)
    context['yesterday_hot_date'] = get_yesterday_hot_date(blog_content_type)
    context['hot_blogs_for_7_days'] = get_7_days_hot_blogs()
    return render(request, 'home.html', context)
Beispiel #5
0
def home(request):
    blog_content_type = ContentType.objects.get_for_model(Blog)
    read_nums, dates = read_seven_days(blog_content_type)
    #获取热门博客缓存数据
    week_hot_dates = cache.get(
        'week_hot_dates')  #获取 一周热门阅读的打的方法(context['week_hot_dates'])
    #如果获取的热门key为空,将写入缓存,不为None提示 user cache
    if week_hot_dates is None:
        get_week_read = get_week_read_nums()  #周热门方法赋值给个变量
        cache.set('week_hot_dates', get_week_read,
                  3600)  #写进缓存 key, value, time(单位:秒)
    else:
        print('user cache')
    context = {}  # 不需要传入其他东西
    context['read_nums'] = read_nums
    context['dates'] = dates
    context['today_hot_dates'] = get_today_hot_date(blog_content_type)
    context['yesterday_hot_dates'] = get_yesterday_hot_date(blog_content_type)
    context['week_hot_dates'] = get_week_read_nums()

    return render(request, 'home.html', context)
Beispiel #6
0
def home(request):
    blog_content_type = ContentType.objects.get_for_model(
        Blog)  #通过contenttype的获取相关模型的类或模型的实例,例如Blog
    dates, read_nums = get_seven_days_read_date(
        blog_content_type)  #传入blog模型返回数据
    today_hot_date = get_today_hot_date(blog_content_type)  #传入blog模型返回数据
    yesterday_hot_date = get_yesterday_hot_date(blog_content_type)

    #获取7天热门的缓存数据
    seven_days_hot_blog = cache.get(
        'seven_days_hot_blog')  #cache.get('缓存名称'),获取缓存数据
    #若无缓存从数据库中获取数据,设置再缓存。
    if seven_days_hot_blog is None:
        seven_days_hot_blog = get_seven_days_hot_blog()
        cache.set('seven_days_hot_blog', seven_days_hot_blog,
                  3600)  #设置缓存,cache.set('缓存名称',缓存的数据,缓存时间)

    context = {}
    context['dates'] = dates
    context['read_nums'] = read_nums
    context['today_hot_date'] = today_hot_date
    context['yesterday_hot_date'] = yesterday_hot_date
    context['seven_days_hot_blog'] = seven_days_hot_blog
    return render(request, 'home.html', context)