Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 'str' object is not callable. How to deal with it?

I have problem with one part of my app. Everything works well, but when I click on a link named 'koszyk' I get:

TypeError at /sklep/koszyk/

'str' object is not callable

Request Method:     GET
Request URL:    http://localhost:8000/sklep/koszyk/
Exception Type:     TypeError
Exception Value:   'str' object is not callable

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response, line 99
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    ['/home/mdawidowski/szkola/django/sklepik', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:    Pi, 5 Czerw 2015 18:49:26 +0200

I've tried everything and I can't find anything... All I know is that problem is with model 'koszyk', because everything else works well. I hope you can help me.

My files: Views.py

# coding: utf-8
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic.simple import direct_to_template
from django.core.mail import send_mail
from django.template import Context, loader
from django.conf import settings
from sklep.models import Produkt
from sklep.forms import ZamowienieForm

def koszyk(request):
    koszyk = request.session.get('koszyk', [])
    produkty = list(Produkt.objects.filter(pk__in=koszyk))

    if request.method == 'POST':
        formularz = ZamowienieForm(request.POST)

        if formularz.is_valid():
            dane = formularz.cleaned_data
            tresc = loader.get_template('sklep/zamowienie.txt').render(Context({'produkty': produkty, 'dane': dane}))

            send_mail('Potwierdzenie zakupu', tresc, settings.EMAIL_SKLEPU, [dane['email']])
            send_mail(u'Zamówienie', tresc, dane['email'], [settings.EMAIL_SKLEPU])

            del request.session['koszyk']

            return HttpResponseRedirect(reverse('sklep_koszyk'))
    else:
        formularz = ZamowienieForm()

    if koszyk:
        kontekst = {'koszyk': produkty, 'formularz': formularz}
    else:
        kontekst = {'koszyk': []}

    return direct_to_template(request, 'sklep/koszyk.html', extra_context = kontekst)

def koszyk_dodaj(request, id_produktu):
    koszyk = request.session.get('koszyk', [])
    if int(id_produktu) not in koszyk:
        koszyk.append(int(id_produktu))
    request.session['koszyk'] = koszyk
    return HttpResponseRedirect(reverse('sklep_koszyk'))

Urls.py

from django.conf.urls.defaults import * from sklep.models import Produkt

urlpatterns = patterns('',
    url(r'^produkty/$', 'django.views.generic.list_detail.object_list', {'queryset': Produkt.objects.all().select_related('kategorie'), 'paginate_by': 1}, "sklep_produkty"),  
    url(r'^koszyk/$', 'koszyk', name="sklep_koszyk"), 
    url(r'^koszyk/dodaj/(\d+)/$', 'koszyk_dodaj', name="sklep_koszyk_dodaj"),
 )

It would be great if someone can help me.

like image 474
RIP Avatar asked Jun 05 '15 17:06

RIP


1 Answers

You are using variable name koszyk multiple times with different values assigned to it.

e.g. your view name is koszyk and in same view you are using same variable name to store result of request.session.get('koszyk', [])

So cleanup your code for such issues.

Also, in your urls.py give complete path for the view instead of just function name, e.g. specify '<app_name>.views.koszyk'

like image 151
Rohan Avatar answered Oct 05 '22 10:10

Rohan