这是我的views.py
from django.shortcuts import render
from site1.models import Flower
from django.core.paginator import Paginator
def home(request):
x = Flower.objects.all()
y = Paginator(x, 12)
pagex = y.page(1)
flowers = list(reversed(pagex.object_list))
c = []
for a in range(3):
c.append(a)
return render(request, 'home.html', {'s_list': flowers, 'links': c})
def home2(request, page):
x = Flower.objects.all()
y = Paginator(x, 12)
pagex = y.page(page)
flowers = list(reversed(pagex.object_list))
c = []
page = int(page)
if page == 1:
for a in range(1, 4):
c.append(a)
elif page == 2:
for a in range(1, 5):
c.append(a)
elif page == y.num_pages - 2:
for a in range(page - 2, page + 2):
c.append(a)
elif page == y.num_pages - 1:
for a in range(page - 2, page + 1):
c.append(a)
elif page == y.num_pages:
for a in range(page - 2, page):
c.append(a)
else:
for a in range(page - 2, page + 2):
c.append(a)
return render(request, 'home.html', {'s_list': flowers, 'links': c})
def type(request, type, page):
x = Flower.objects.all().filter(tip=type)
y = Paginator(x, 12)
pagex = y.page(page)
flowers = list(reversed(pagex.object_list))
c = []
page = int(page)
if page == 1:
for a in range(1, 4):
c.append(a)
elif page == 2:
for a in range(1, 5):
c.append(a)
elif page == y.num_pages - 2:
for a in range(page - 2, page + 2):
c.append(a)
elif page == y.num_pages - 1:
for a in range(page - 2, page + 1):
c.append(a)
elif page == y.num_pages:
for a in range(page - 2, page):
c.append(a)
else:
for a in range(page - 2, page + 2):
c.append(a)
return render(request, 'home.html', {'s_list': flowers, 'links': c})
这是urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'site1.views.home', name='home'),
url(r'^(\d+)$', 'site1.views.home2'),
url(r'^gul/(\d+)$', 'site1.views.type'),
# url(r'^mysite1/', include('mysite1.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls))
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
这是错误
TypeError at /gul/1
type() takes exactly 3 arguments (2 given)
Request Method: GET
Request URL: http://127.0.0.1:8000/gul/1
Django Version: 1.4.3
Exception Type: TypeError
例外值:
type()只需3个参数(给定2个)
例外位置:/Library/Python/2.7/site-packages/django/core/handlers/base.py in get_response,第111行
Python可执行文件:/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python Python版本:2.7.2
我不知道为什么会出现这种错误? 我将url添加到urls.py 我添加了链接到模板home.html
如何解决此问题?
答案 0 :(得分:2)
在views.py
中,您的type
视图只有3个参数:
def type(request, type, page):
....
但是在urls.py
中,你允许它只获得2个参数:
url(r'^gul/(\d+)$', 'site1.views.type'),
在这种情况下,(\d+)
将被视为type
视图的第二个参数,因为request是每个基于函数的视图的默认参数。
你应该做的是减少视图中的参数,如:
def type(request, page):
...
并在urls.py
中为您的参数指定一些名称:
url(r'^gul/(?P<page>\d+)$', 'site1.views.type'),
(?P<id>\d+)
将匹配\d+
模式,并将其分配给page
。
如果您仍希望在视图中保留3个参数,则应更改URL的模式:
url(r'^gul/(?P<type>\d+)/(?P<page>\d+)$', 'site1.views.type'),
因此,您的网址应该类似于/gul/2/1
,在您看来,您会得到type
= 2和page
= 1。
希望它有所帮助。