gpt4 book ai didi

python - Django 'str' 对象不可调用。如何处理?

转载 作者:太空狗 更新时间:2023-10-30 02:59:57 24 4
gpt4 key购买 nike

我的应用程序的一部分有问题。一切正常,但当我点击一个名为“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”有问题,因为其他一切都很好。我希望你能帮助我。

我的文件: View .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'))

网址.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com