2015-06-05 65 views
2

我的應用程序的一部分出現問題。一切正常,但是當我點擊一個名爲「koszyk」我得到的鏈接:Django'str'對象不可調用。如何處理它?

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 

我用盡了一切,我無法找到任何東西......我所知道的是,問題是與模型「 koszyk',因爲一切運作良好。我希望你能幫助我。

我的文件: 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"), 
) 

這將是巨大的,如果有人能幫助我。

+1

您應該提供視圖的完整路徑。 –

+0

Django框架試圖通過提供的'string'來查找'view',因此你將不得不提及全視圖路徑。如果'django.views.generic.list_detail.object_list'得到解決,您將需要交叉驗證。 –

+0

此外,在Django 1.8中,您不能再使用字符串作爲可調用函數來查看函數:https://docs.djangoproject.com/en/1.8/releases/1.8/#passing-a-string-as-view-to-網址 – Brandon

回答

2

您使用變量名稱koszyk多次分配不同的值。

例如您的查看名稱是koszyk,並且在同一視圖中,您使用的是相同的變量名稱來存儲結果request.session.get('koszyk', [])

因此,清理您的代碼以解決此類問題。

此外,在您的urls.py爲視圖提供完整路徑,而不僅僅是函數名稱,例如指定'<app_name>.views.koszyk'

1

這是可能的,你要替換函數本身的命名空間:

def koszyk(request): 
    koszyk = request.session.get('koszyk', []) 

這第二行應該切實從重新定義你聲明的功能koszyk,到任何字符串趴在會議圍繞。然後,當您的程序稍後嘗試將koszyk作爲視圖服務時,它會突然顯示一個字符串,而不是您聲明的函數