1

这是我的意见.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

我该如何解决这个问题?

4

1 回答 1

2

views.pytype看来,恰好需要 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'),

所以你的 URL 应该是这样的/gul/2/1,在你看来,你会得到type= 2 和page= 1。

希望能帮助到你。

于 2013-08-23T07:46:24.120 回答