vids_list = Video.objects.all()
page_robot = Paginator(vids_list, 9)
分页机器人=Paginator(数据,每页几个)
vids_list = page_robot.page(page_num)
from django.db import models
from faker import Factory
# Create your models here.
class Video(models.Model):
title = models.CharField(null=True, blank=True, max_length=300)
content = models.TextField(null=True, blank=True)
url_image = models.URLField(null=True, blank=True)
editors_choice = models.BooleanField(default=False)
def __str__(self):
return self.title
# f = open('/Users/Hou/Desktop/web_url.txt','r')
# for url in f.readlines():
# v = Video(
# title=fake.text(max_nb_chars=90),
# content=fake.text(max_nb_chars=3000),
# url_image=url,
# editors_choice=fake.pybool()
# )
# v.save()
from django.shortcuts import render, redirect, HttpResponse
from website.models import Video
from django.core.paginator import Paginator
# Create your views here.
def listing(request):
context = {}
vids_list = Video.objects.all()
page_robot = Paginator(vids_list, 9)
vids_list = page_robot.page(request.GET.get('page'))
context['vids_list'] = vids_list
return render(request, 'listing.html', context)
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="{% static 'css/list_custom.css' %}" media="screen" title="no title" charset="utf-8">
head>
<body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="{% static 'images/tenlogo.png' %}" alt="">
div>
div>
<div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui avatar image">
<img src="{% static 'images/default.png' %}" alt="">
div>
<span>adminspan>
h5>
div>
<div class="item">
<a href="#logout/" class="ui inverted circular button">Logouta>
div>
div>
div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="active item" href="#list/all">
All
a>
<a class="item">
New
a>
<a class="item" href="#list/editors">
Editor's
a>
div>
div>
<div class="ui basic segment container content">
<div class="ui three column grid">
{% for article in vids_list %}
<div class="column">
<a class="ui fluid card" href="{% url 'list' %}">
<div class="image">
<img src="{{ article.img }}" alt="" style="height:200px;object-fit: cover;">
div>
a>
<div class="title header" href="{% url 'list' %}">{{ article.title }}div>
<i class="icon grey unhide">i>
<span style="color:#bbbbbb">{{ article.views }}span>
<span class="" style="color:rgb(226, 226, 226)">|span>
<i class="icon grey checkmark">i>
<span style="color:#bbbbbb"> {{ article.favs }} people got itspan>
div>
{% endfor %}
div>
div>
<div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu">
<a href="#" class="item">
<i class="icon red left arrow">i>
a>
<a href="#" class="item">
<i class="icon red right arrow">i>
a>
div>
div>
body>
html>
通过输入http://127.0.0.1:8000/list/?page=999直接访问。
from django.shortcuts import render, Http404
from website.models import Video
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.
def listing(request):
context = {}
vids_list = Video.objects.all()
page_robot = Paginator(vids_list, 9)
page_num = request.GET.get('page')
try:
vids_list = page_robot.page(page_num)
except EmptyPage:
vids_list = page_robot.page(page_robot.num_pages)
#raise Http404('EmptyPage')
except PageNotAnInteger:
vids_list = page_robot.page(1)
context['vids_list'] = vids_list
return render(request, 'listing.html', context)
<div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu">
{% if vids_list.has_previous %}
<a href="?page={{ vids_list.previous_page_number }}" class="item">
<i class="icon red left arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.start_index }}" class="disabled item">
<i class="icon left arrow">i>
a>
{% endif %}
{% if vids_list.has_next %}
<a href="?page={{ vids_list.next_page_number }}" class="item">
<i class="icon red right arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.end_index }}" class="disabled item">
<i class="icon right arrow">i>
a>
{% endif %}
div>
div>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="{% static 'css/list_custom.css' %}" media="screen" title="no title" charset="utf-8">
head>
<body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="{% static 'images/tenlogo.png' %}" alt="">
div>
div>
<div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui avatar image">
<img src="{% static 'images/default.png' %}" alt="">
div>
<span>adminspan>
h5>
div>
<div class="item">
<a href="#logout/" class="ui inverted circular button">Logouta>
div>
div>
div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="active item" href="#list/all">
All
a>
<a class="item">
New
a>
<a class="item" href="#list/editors">
Editor's
a>
div>
div>
<div class="ui basic segment container content">
<div class="ui three column grid">
{% for article in vids_list %}
<div class="column">
<a class="ui fluid card" href="{% url 'list' %}">
<div class="image">
<img src="{{ article.img }}" alt="" style="height:200px;object-fit: cover;">
div>
a>
<div class="title header" href="{% url 'list' %}">{{ article.title }}div>
<i class="icon grey unhide">i>
<span style="color:#bbbbbb">{{ article.views }}span>
<span class="" style="color:rgb(226, 226, 226)">|span>
<i class="icon grey checkmark">i>
<span style="color:#bbbbbb"> {{ article.favs }} people got itspan>
div>
{% endfor %}
div>
div>
<div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu">
{% if vids_list.has_previous %}
<a href="?page={{ vids_list.previous_page_number }}" class="item">
<i class="icon red left arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.start_index }}" class="disabled item">
<i class="icon left arrow">i>
a>
{% endif %}
{% if vids_list.has_next %}
<a href="?page={{ vids_list.next_page_number }}" class="item">
<i class="icon red right arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.end_index }}" class="disabled item">
<i class="icon right arrow">i>
a>
{% endif %}
div>
div>
body>
html>
from django.conf.urls import url
from django.contrib import admin
from website.views import listing
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^list/$', listing, name='list'),
url(r'^list/(?P[A-Za-z]+)$' , listing, name='list'),
]
from django.shortcuts import render, Http404
from website.models import Video
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.
def listing(request, cate=None):
context = {}
if cate is None:
vids_list = Video.objects.all()
if cate == 'editors':
vids_list = Video.objects.filter(editors_choice=True)
else:
vids_list = Video.objects.all()
page_robot = Paginator(vids_list, 9)
page_num = request.GET.get('page')
try:
vids_list = page_robot.page(page_num)
except EmptyPage:
vids_list = page_robot.page(page_robot.num_pages)
#raise Http404('EmptyPage')
except PageNotAnInteger:
vids_list = page_robot.page(1)
context['vids_list'] = vids_list
return render(request, 'listing.html', context)
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item" >
All
a>
<a class="item">
New
a>
{% if 'editors' in request.path %}
<a class="active item" href="{% url 'list' %}editors">
Editor's
a>
{% else %}
<a class="item" href="{% url 'list' %}editors">
Editor's
a>
{% endif %}
div>
div>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="{% static 'css/list_custom.css' %}" media="screen" title="no title" charset="utf-8">
head>
<body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="{% static 'images/tenlogo.png' %}" alt="">
div>
div>
<div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui avatar image">
<img src="{% static 'images/default.png' %}" alt="">
div>
<span>adminspan>
h5>
div>
<div class="item">
<a href="#logout/" class="ui inverted circular button">Logouta>
div>
div>
div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item" >
All
a>
<a class="item">
New
a>
{% if 'editors' in request.path %}
<a class="active item" href="{% url 'list' %}editors">
Editor's
a>
{% else %}
<a class="item" href="{% url 'list' %}editors">
Editor's
a>
{% endif %}
div>
div>
<div class="ui basic segment container content">
<div class="ui three column grid">
{% for article in vids_list %}
<div class="column">
<a class="ui fluid card" href="{% url 'list' %}">
<div class="image">
<img src="{{ article.img }}" alt="" style="height:200px;object-fit: cover;">
div>
a>
<div class="title header" href="{% url 'list' %}">{{ article.title }}div>
<i class="icon grey unhide">i>
<span style="color:#bbbbbb">{{ article.views }}span>
<span class="" style="color:rgb(226, 226, 226)">|span>
<i class="icon grey checkmark">i>
<span style="color:#bbbbbb"> {{ article.favs }} people got itspan>
div>
{% endfor %}
div>
div>
<div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu">
{% if vids_list.has_previous %}
<a href="?page={{ vids_list.previous_page_number }}" class="item">
<i class="icon red left arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.start_index }}" class="disabled item">
<i class="icon left arrow">i>
a>
{% endif %}
{% if vids_list.has_next %}
<a href="?page={{ vids_list.next_page_number }}" class="item">
<i class="icon red right arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.end_index }}" class="disabled item">
<i class="icon right arrow">i>
a>
{% endif %}
div>
div>
body>
html>
from django.db import models
from faker import Factory
# Create your models here.
class Video(models.Model):
title = models.CharField(null=True, blank=True, max_length=300)
content = models.TextField(null=True, blank=True)
url_image = models.URLField(null=True, blank=True)
cover = models.FileField(upload_to='cover_image', null=True)
editors_choice = models.BooleanField(default=False)
def __str__(self):
return self.title
STATIC_URL = '/static/'
MEDIA_URL = '/upload/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload').replace("//","/")
from django.conf.urls import url
from django.contrib import admin
from website.views import listing
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^list/$', listing, name='list'),
url(r'^list/(?P[A-Za-z]+)$' , listing, name='list'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#在本地运行时使用
<div class="image">
{% if article.cover %}
<img src="/upload/{{ article.cover }}" alt="" style="height:200px;object-fit: cover;">
{% else %}
<img src="{{ article.url_image }}" alt="" style="height:200px;object-fit: cover;">
{% endif %}
div>
{% load staticfiles %}
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>title>
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="{% static 'css/list_custom.css' %}" media="screen" title="no title" charset="utf-8">
head>
<body>
<div class="ui inverted top fixed menu borderless red menu">
<div class="header item">
<div class="ui image">
<img src="{% static 'images/tenlogo.png' %}" alt="">
div>
div>
<div class="right menu">
<div class="item">
<h5 class="ui inverted header">
<div class="ui avatar image">
<img src="{% static 'images/default.png' %}" alt="">
div>
<span>adminspan>
h5>
div>
<div class="item">
<a href="#logout/" class="ui inverted circular button">Logouta>
div>
div>
div>
<div class="ui inverted segment container nav">
<div class="ui three borderless item menu">
<a class="item" >
All
a>
<a class="item">
New
a>
{% if 'editors' in request.path %}
<a class="active item" href="{% url 'list' %}editors">
Editor's
a>
{% else %}
<a class="item" href="{% url 'list' %}editors">
Editor's
a>
{% endif %}
div>
div>
<div class="ui basic segment container content">
<div class="ui three column grid">
{% for article in vids_list %}
<div class="column">
<a class="ui fluid card" href="{% url 'list' %}">
<div class="image">
{% if article.cover %}
<img src="/upload/{{ article.cover }}" alt="" style="height:200px;object-fit: cover;">
{% else %}
<img src="{{ article.url_image }}" alt="" style="height:200px;object-fit: cover;">
{% endif %}
div>
a>
<div class="title header" href="{% url 'list' %}">{{ article.title }}div>
<i class="icon grey unhide">i>
<span style="color:#bbbbbb">{{ article.views }}span>
<span class="" style="color:rgb(226, 226, 226)">|span>
<i class="icon grey checkmark">i>
<span style="color:#bbbbbb"> {{ article.favs }} people got itspan>
div>
{% endfor %}
div>
div>
<div class="ui center aligned very padded vertical segment container">
<div class="ui pagination menu">
{% if vids_list.has_previous %}
<a href="?page={{ vids_list.previous_page_number }}" class="item">
<i class="icon red left arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.start_index }}" class="disabled item">
<i class="icon left arrow">i>
a>
{% endif %}
{% if vids_list.has_next %}
<a href="?page={{ vids_list.next_page_number }}" class="item">
<i class="icon red right arrow">i>
a>
{% else %}
<a href="?page={{ vids_list.end_index }}" class="disabled item">
<i class="icon right arrow">i>
a>
{% endif %}
div>
div>
body>
html>