LoginSignup
0
0

More than 3 years have passed since last update.

Django ページネーションについて教えていただきたいです

Last updated at Posted at 2020-05-31

Python Djangoという参考書で勉強していたのですが
4−3ページネーションの「ページの移動はどうするの?」で躓いてます。
firstはちゃんと動くのですが、prev,next,lastがどうしてもPage not foundになってしまいます。
何方か教えてくれませんか?
ソースはこんな感じです
初投稿なのでうまく使えてないと思います。すみません
django_app/hello/url.py
from django.urls import path
from . import views

urlpatterns=[
path('',views.index,name='index'),
path('int:num',views.index,name='index'),
path('create',views.create,name='create'),
path('edit/int:num',views.edit,name='edit'),
path('delete/int:num',views.delete,name='delete'),
path('find',views.find,name='find'),
path('check',views.check,name='check'),
]

django_app/hello/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from .models import Friend
from .forms import FriendForm
from .forms import FindForm
from django.db.models import Q
from django.db.models import Count,Sum,Avg,Min,Max
from .forms import CheckForm
from django.core.paginator import Paginator

def index(request,num=1):
data = Friend.objects.all()
page = Paginator(data,3)
params = {
'title': 'Hello',
'message':'',
'data': page.get_page(num),
}
return render(request,'hello/index.html',params)

create model

def create(request):
if (request.method == 'POST'):
obj = Friend()
friend = FriendForm(request.POST,instance=obj)
friend.save()
return redirect(to='/hello')
params = {
'title' : 'Hello',
'form' : FriendForm(),
}
return render(request,'hello/create.html',params)
def edit(request,num):
obj = Friend.objects.get(id=num)
if (request.method == 'POST'):
friend = FriendForm(request.POST,instance=obj)
friend.save()
return redirect(to='/hello')
params={
'title' : 'Hello',
'id' : num,
'form' : FriendForm(instance=obj),
}
return render(request,'hello/edit.html',params)
def delete(request,num):
friend = Friend.objects.get(id=num)
if (request.method == 'POST'):
friend.delete()
return redirect(to='/hello')
params = {
'title' : 'Hello',
'id' : num,
'obj' : friend,
}
return render(request,'hello/delete.html',params)

def find(request):
if (request.method == 'POST'):
msg = request.POST['find']
form = FindForm(request.POST)
sql = 'select * from hello_friend'
if (msg != ''):
sql += ' where ' + msg
data = Friend.objects.raw(sql)
msg = sql
else:
msg = 'search words...'
form = FindForm()
data =Friend.objects.all()
params = {
'title' : 'Hello',
'message' : msg,
'form' :form,
'data' :data,
}
return render(request,'hello/find.html',params)
def check(request):
params={
'title' : 'Hello',
'message' : 'check validation.',
'form' : CheckForm(),
}
if (request.method == 'POST'):
obj = Friend()
form = FriendForm(request.POST,instance=obj)
params['form'] = form
if (form.is_valid()):
params['message'] = 'OK!'
else:
params['message'] = 'no good.'
return render(request,'hello/check.html',params)

django_app/hello/template/hello/index.html
{% load static %}
<!DOCTYPE html>



{{title}}
href="{% static 'hello/css/style.css' %}" />


{{title}}


{{message|safe}}










{% for item in data %}







{% endfor %}
id name age mail birthday
{{item.id}} {{item.name}} {{item.age}} {{item.mail}} {{item.birthday}}


{% if data.has_previous %}
«first
«prev
{% endif %}

[{{data.number}}/{{data.paginator.num_pages}}]

{% if data.has_next %}
next»
last»
{% endif %}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0