I was studying with a reference book called Python Django I'm stumbling on "How do I move pages?" In 4-3 Pagination. first works fine, but prev, next, and last are inevitably Page not found. Can you tell me who you are? The source looks like this It's my first post, so I don't think it can be used well. I'm sorry 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 %}
{{message|safe}}
id | name | age | birthday | |
---|---|---|---|---|
{{item.id}} | {{item.name}} | {{item.age}} | {{item.mail}} | {{item.birthday}} |
Recommended Posts