malisit
malisit

Reputation: 1258

Django, function takes exactly 2 arguments (1 given)

I have a function in views.py like this:

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:

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

Upvotes: 0

Views: 6171

Answers (2)

0n10n_
0n10n_

Reputation: 402

The issue is that when you call '/' you are not assigning the slug argument. The trick is to give a default value to the slug argument i.e:

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

Upvotes: 0

Aaron Lelevier
Aaron Lelevier

Reputation: 20780

The hepsi view uses the slug kwarg, so the slug is required in the url:

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

Upvotes: 2

Related Questions