我的应用程序一部分有问题。一切正常,但是当我单击名为“koszyk”的链接时,我得到:
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"),
)
如果有人可以帮助我,那将是很棒的。
最佳答案
您多次使用变量名koszyk
并为其分配了不同的值。
例如您的 View 名称是koszyk
,并且在同一 View 中,您使用相同的变量名来存储request.session.get('koszyk', [])
的结果
因此,请针对此类问题清理您的代码。
另外,在urls.py
中,为 View 提供完整的路径,而不仅仅是函数名,例如指定'<app_name>.views.koszyk'
关于python - Django 'str'对象不可调用。怎么处理呢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30672302/