查看函数未在Django中调用

时间:2012-01-09 03:19:37

标签: python django

这是view.py. show_checkout被调用并一直运行到return HttpResponseRedirect('/receipt/'),但出于某种原因,receipt视图未被触发。没有任何打印报表正在运作:

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core import urlresolvers
from django.http import HttpResponseRedirect

from ecomstore.checkout.forms import CheckoutForm
from ecomstore.checkout.models import Order, OrderItem
from ecomstore.checkout import checkout
from ecomstore.cart import cart
from django.core.context_processors import csrf

# Create your views here.
def show_checkout(request, template_name='checkout/checkout.html'):
    c = {}
    c.update(csrf(request))
    if cart.is_empty(request):
        cart_url = urlresolvers.reverse('show_cart')
        return HttpResponseRedirect(cart_url)
    if request.method == 'POST':
        postdata = request.POST.copy()
        form = CheckoutForm(postdata)
        if form.is_valid():
            response = checkout.process(request)
            order_number = response.get('order_number',0)
            print 'this is the order number: ' + str(order_number)
            error_message = response.get('message','')
            if order_number:
                request.session['order_number'] = order_number
#                receipt_url = urlresolvers.reverse('checkout_receipt')
                print request.session['order_number']
                return HttpResponseRedirect('/receipt/')

        else:
            error_message = 'Correct the errors below'
    else:
        form = CheckoutForm()
    page_title = 'Checkout'
    return render_to_response(template_name, locals(), context_instance= RequestContext(request))

##Currently this doesn't seem to be running
def receipt(request, template_name='checkout/receipt.html'):
    order_number = request.session.get('order_number','')
    print 'this is receipt order number: ' + str(order_number)
    if order_number:
        print 'in order_number'
        order = Order.objects.filter(id=order_number)[0]
        order_items = OrderItem.objects.filter(order=order)
        del request.session['order_number']
    else:
        print 'not in order number'
        cart_url = urlresolvers.reverse('show_cart')
        return HttpResponseRedirect(cart_url) 
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))

这是checkout.urls:

from django.conf.urls.defaults import *
from ecomstore import settings

urlpatterns = patterns('ecomstore.checkout.views',
                       (r'^$','show_checkout',{'template_name':'checkout/checkout.html','SSL':settings.ENABLE_SSL},'checkout'),
                       (r'^receipt/$','receipt',{'template_name':'checkout/receipt.html','SSL':settings.ENABLE_SSL},'checkout_receipt'),
)

和urls.py:

urlpatterns = patterns('',

                       # Examples:
    # url(r'^$', 'ecomstore.views.home', name='home'),
    # url(r'^ecomstore/', include('ecomstore.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
                       url(r'^admin/', include(admin.site.urls)),
                       (r'^', include('catalog.urls')),
                       (r'^cart/$', include('cart.urls')),
                       (r'^checkout/$', include('checkout.urls')),
                       (r'^receipt/$', include('checkout.urls')),

)

重要提示:此处目前正转发至localhost/cart

2 个答案:

答案 0 :(得分:2)

在你的主urls.py中,不要在正则表达式中使用“$”。这似乎已经为我解决了。

答案 1 :(得分:1)

我的猜测是你的意思/收据/收据