Django,函数只需2个参数(给定1个)

时间:2014-07-14 14:03:32

标签: python django function django-views typeerror

我在views.py中有一个函数,如下所示:

gunlist = []

def hepsi(request, slug):

    basliklar = Baslik.objects.filter(active=True).order_by('-updated')
    for i in basliklar:
        i = Baslik.objects.get(slug=slug)
        entryler = i.entry_set.all()
        ent1 = entryler.latest('id')
        ent2 = ent1.updated
        ent3 = str(ent2).split(" ")
        zaman1 = date.today()
        zaman2 = str(zaman1).split(" ")
        zamangun = zaman2[0]
        entgun = ent3[0]

        if entgun == zamangun:
            gunlist.append(i)


    cta = {'form2': form2, 'basliklar': basliklar, 'entryler': entryler, 'baslik': baslik, 'ent1': ent1, 'ent2': ent2, 'entgun': entgun, 'zamangun': zamangun}

    return render(request, "base.html", cta)

我试图比较对象的日期和当前日期。当我运行服务器并转到链接时。它引发了这个错误:

TypeError at /
hepsi() takes exactly 2 arguments (1 given)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.6.5
Exception Type: TypeError
Exception Value:    
hepsi() takes exactly 2 arguments (1 given)
Exception Location: /Users/malisit/Django/sozluk/lib/python2.7/site-packages/django/core/handlers/base.py in get_response, line 112
Python Executable:  /Users/malisit/Django/sozluk/bin/python
Python Version: 2.7.5

这有什么不对?我该怎么做才能解决这个问题?感谢。

这是urls.py部分:

url(r'^$', 'hepsi', name = "hepsiliste")

2 个答案:

答案 0 :(得分:2)

hepsi视图使用slug kwarg,因此网址中需要slug

url(r'^(?P<slug>[-w]+)/$', 'hepsi', name = "hepsiliste")

答案 1 :(得分:0)

问题在于,当你调用'/'时,你没有分配slug参数。 诀窍是给slug参数赋一个默认值,即:

 def hepsi(request, slug=None):
    #your logic