这是我们《手把手教你用django搭建博客》系类的第三篇,这篇我们接上篇内容,解决如何实现首页文章分页的功能, 要实现分页要用到django的一个插件, 所以要先安装django_pagination,安装方式随意了,pip或者用自己的IDE功能来安装就可以,安装完毕后我们修改下配置文件就可以实现了,说实话,我在没用之前,真没见过实现分页如此简单的,好,废话不多说,我们开始,打开myblog下seettings.py文件,修改INSTALLED_APPS,MIDDLEWARE_CLASSES,TEMPLATE_CONTEXT_PROCESSORS这三段的内容,所以最后你的settings.py文件看起来应该像下面一样(算了,就把我的settings.py全部贴出来了吧):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
""" Django settings for mysite1 project. For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'lg_!ngsts^nd4*6*+ze(yh%fk)bc!pgxzc*vm(@b4zw9)409%6' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', 'pagination', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'pagination.middleware.PaginationMiddleware', ) ROOT_URLCONF = 'myblog.urls' WSGI_APPLICATION = 'myblog.wsgi.application' # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.request', ) # Internationalization # https://docs.djangoproject.com/en/1.6/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.6/howto/static-files/ STATIC_URL = '/static/' |
设置完settings.py文件后,我们需要再修改下我们的模板文件,打开index.html,修改内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
{% extends 'base.html' %} {% load pagination_tags %} {% block content %} {% autopaginate posts 2 %} {% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.timestamp|date:"1, F jS" }}</p> <p>{{ post.body }}</p> {% endfor %} <span style="padding-right: 60;float:right;list-style-type:none;">{% paginate %}</span> {% endblock %} |
默认的{% paginante %}是在左边,我加入了<span> 标签和行css样式,这样就移到了右边,这样就完成全部的分页功能了,什么??是,你没听错,真的没有了,所以我开篇就说了,用django设置分页如此简单,平生我也是第一次见到,怎么?不信?不信启动项目,访问下:
http://127.0.0.1:8000/blog/
看看是否已经实现了分页,这时候你看完,心头一乐,发现还TM实现了,转眼间你又心头一愣,哎,不对,这这跟开篇的第一张图不一样呀,我这分页是英文表示的,可你显示的中文:上一页,下一页呀,好吧,兄弟,算你好眼力,我们接下来就看看如何修改下默认的样式,打开pagination.html文件(在C:\Python27\Lib\site-packages\pagination\templates\pagination下),修改后如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{% if is_paginated %} {% load i18n %} <div class="pagination"> {% if page_obj.has_previous %} <a href="?page={{ page_obj.previous_page_number }}{{ getvars }}{{ hashtag }}" class="prev">‹‹ {% trans "上一页" %}</a> {% else %} <span class="disabled prev">‹‹ {% trans "上一页" %}</span> {% endif %} {% for page in pages %} {% if page %} {% ifequal page page_obj.number %} <span class="current page">{{ page }}</span> {% else %} <a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a> {% endifequal %} {% else %} ... {% endif %} {% endfor %} {% if page_obj.has_next %} <a href="?page={{ page_obj.next_page_number }}{{ getvars }}{{ hashtag }}" class="next">{% trans "下一页" %} ››</a> {% else %} <span class="disabled next">{% trans "下一页" %} ››</span> {% endif %} </div> {% endif %} |
把对应英文的显示换成中文就可以了,这次是真的完成了。
好吧,到现在你可以去庆祝下你所学的东西了,既然这么高兴,你可以去喝二杯,当然也可以去请找妹子吃个饭,没准还能….你懂的。