简体   繁体   English

Django,函数正好接受2个参数(给定1个)

[英]Django, function takes exactly 2 arguments (1 given)

I have a function in views.py like this: 我在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)

I'm trying to compare an object's date and current date. 我正在尝试比较对象的日期和当前日期。 When I run server and go to the link. 当我运行服务器并转到链接时。 It raises this error: 它引发此错误:

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

What's wrong about this? 这怎么了 What should I do to fix this? 我应该怎么做才能解决这个问题? Thanks. 谢谢。

This is the urls.py part: 这是urls.py部分:

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

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

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

The issue is that when you call '/' you are not assigning the slug argument. 问题是,当您调用“ /”时,您并未分配slug参数。 The trick is to give a default value to the slug argument ie: 诀窍是为slug参数提供默认值,即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM