django paging system

Keywords: Database MySQL Django Python

1. Create the project and set it in settings.py and other basic operations

2. Add data to the database. This database uses mysql database,

If using mysql database, pay attention to

① mysql should be introduced into the init file in the created project file

import pymysql
pymysql.install_as_MySQLdb()

And set the relevant information for the database in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':'django',
        'HOST':'localhost',
        'USER':'root',
        'PASSWORD':'891811395',
        'PORT':3306
    }
}

② set the fields in models.py under the app folder for database table building

from django.db import models

# Create your models here.
class Goods(models.Model):
    name = models.CharField(max_length=100)
    des = models.CharField(max_length=1000)

    class Meta:
        db_table = 'goods'

 

def index(request):

Add data with random number

import random
def index(request):

    # for x in range(200):
    #     good = Goods(name='good%s'%x
    #                  , des = 'this product is of good quality and low price. Now it only needs {} Yuan'. Format (random. RandInt (10100)))
    #     good.save()
    return HttpResponse('Data added successfully')

3.

def select(request): in this method, obtain the total data good list = Goods.objects.all ()

4. Set pagination display rule: Paginator = Paginator (good menu, 12, 3) means: 12 pieces of data on a page, when the last piece of data is less than 3 pieces, combine the data to the previous page

5. There are three kinds of page values in the website

    try:
        # get request method get() gets the value corresponding to the specified key value
        # Get the value of index, if not, set to use the default value of 1
        #              If there is value, pass number to index.html below
        num = request.GET.get('index','1')
        number = paginator.page(num)
    except PageNotAnInteger:
        # If the number of page numbers entered is not an integer, the first page data is displayed
        number = paginator.page(1)
    except EmptyPage:
        # If the page number is not within the current page number range, the last page is displayed
        # Paginator.num ABCD pages get the current total number of pages
        # paginator.page() gets a specified page
        number = paginator.page(paginator.num_pages)
    # ABCD current page number and current page data transferred to html
    return render(request,'index.html',{'page':number,'paginator':paginator})

6. Setup page

In index.html, when using static resources, you should not only load static resources in HTML pages, but also set them in settings.py

# Set the static resources in the app
STATIC_URL = '/static/'
# It sets the static resources in the project
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
    <title>TaoBao{{page}}</title>
</head>
<body>
        {% for good in page.object_list %}
            <h4>{{good.name}} - {{good.des}}</h4>
        {% endfor %}

        <ul class="pagination">
            {# If the current page has a previous page #}
            {% if page.has_previous %}
                <li>
                    {# click a Tab jump to previous link ?index= Parameters spliced after address #}
                    <a href="?index={{page.previous_page_number}}">Previous page</a>
                </li>
            {% else %}
                {# If there is no previous page, the button cannot be clicked #}
                <li class="disabled">
                    <a href="#"> previous</a>
                </li>
            {% endif %}

            {% for page_number in paginator.page_range %}
                {# page.number Get the page number of the current page #}
                {% if page_number == page.number %}
                    <li class="active" >
                        <a href="?index={{page_number}}">{{page_number}}</a>
                    </li>
                {% else %}
                    <li>
                        <a href="?index={{page_number}}">{{page_number}}</a>
                    </li>
                {% endif %}
            {% endfor%}

            {% if page.has_next %}
                <li>
                    <a href="?index={{page.next_page_number}}">next page</a>
                </li>
            {% else %}
                <li class="disabled">
                    <a href="#"> next page</a>
                </li>

            {% endif %}
        </ul>

    <script src="{% static 'js/jquery.js' %}"></script>
    <script src="{% static 'js/bootstrap.js' %}"></script>
</body>
</html>

7. Before running the project, check the command python manage.py makemigrations

Then migrate the file terminal command: python manage.py migrate

Start the running server python manage.py runserver (the default 8000 can also be set by itself, such as 8080)

Enter the web address localhost:8000 on the web page for operation

 

Posted by Mistat2000 on Thu, 02 Jan 2020 15:00:01 -0800