g电子商务网站

时间:2018-09-23 02:07:53

标签: python django wagtail

是否可以通过覆盖serve()并在其中放置以下视图信息来在Wagtail上构建购物车和条纹支付系统?以下views.py和urls.py是我想在我的wagtail网站中建立的电子商务网站的一部分。我只是很困惑,因为我习惯于使用urls.py和views.py。听起来比尝试在w中做起来要容易得多。

编辑:我目前有一个使用Wagtail的博客网站,但是要添加付款处理系统和可购买的内容,我将创建一个完全独立的django应用程序,并运行不带w子链接到子域的vanilla django。

Views.py的示例

def paymentMethod(request):
    return render(
        request,
        'checkout/payment_method.html',
    )

def get_user_pending_order(request):
    # get order for the correct user
    user_profile = get_object_or_404(Profile, user=request.user)
    order = Order.objects.filter(owner=user_profile, is_ordered=False)
    if order.exists():
        # get the only order in the list of filtered orders
        return order[0]
    return 0


def add_to_cart(request, **kwargs):
    #get the user Profile
    user_profile = get_object_or_404(Profile, user=request.user)

    #filter products for id
    product = Product.objects.filter(id=kwargs.get('item_id', "")).first()

    #check if the user already owns the product
    if product in request.user.user_profile.merchandise.all():
        messages.info(request, "You already own this product")
        return redirect(reverse('product_list'))
    #create OrderItem of the selected product
    order_item, status = OrderItem.objects.get_or_create(product=product)
    #create order associate with the user
    user_order, status = Order.objects.get_or_create(owner=user_profile, is_ordered=False)
    user_order.items.add(order_item)
    if status:
        #generate a reference code
        user_order.ref_code = generate_order_id()
        user_order.save()

    #show confirmataion message and redirect to same page
    messages.info(request, "item added to cart")
    #return redirect(reverse('product_list'))
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

@login_required
def delete_from_cart(request, item_id):
    item_to_delete = OrderItem.objects.filter(pk=item_id)
    if item_to_delete.exists():
        item_to_delete[0].delete()
        messages.info(request, "Item has been removed from shopping cart")
    return redirect(reverse('checkout:order_summary'))

@login_required
def order_details(request, **kwargs):
    count = OrderItem.objects.all().count()
    existing_order = get_user_pending_order(request)
    context = {
        'order': existing_order,
        'count': count
    }
    return render(request, 'checkout/order_summary.html', context)


#class UserDetailView(LoginRequiredMixin, DetailView):
#    model = User
    # These next two lines tell the view to index lookups by username
#    slug_field = 'username'
#    slug_url_kwarg = 'username'

@login_required
def checkout(request):

    count = OrderItem.objects.all().count()
    existing_order = get_user_pending_order(request)

    #stripe
    publishKey = settings.STRIPE_PUBLISHABLE_KEY
    customer_id = request.user.user_stripe.stripe_id
    if request.method == 'POST':
        token = request.POST['stripeToken']
        print (token)
                # Token is created using Checkout or Elements!
        # Get the payment token ID submitted by the form:
        try:

            charge = stripe.Charge.create(
                amount=999,
                currency='usd',

                description='Example charge',
                source=token,

            )
            return redirect(reverse('checkout:update_records',
                        kwargs={
                            'token': token
                        })
                    )
        except stripe.error.CardError as e:
            message.info(request, "Your card has been declined.")


    context = {
        'order': existing_order,
        'publishKey': publishKey,
        'count': count
    }
    return render(request, 'checkout/checkout.html', context)


def update_transaction_records(request, token):
  # get the order being processed
    order_to_purchase = get_user_pending_order(request)

    # update the placed order
    order_to_purchase.is_ordered=True
    order_to_purchase.date_ordered=datetime.datetime.now()
    order_to_purchase.save()

    # get all items in the order - generates a queryset
    order_items = order_to_purchase.items.all()

    # update order items
    order_items.update(is_ordered=True, date_ordered=datetime.datetime.now())

Urls.py

urlpatterns = [

    #Shopping Cart
    url(r'^payment-method/$', views.paymentMethod, name='payment-method'),
    url(r'^add-to-cart/(?P<item_id>[-\w]+)/$', views.add_to_cart, name='add_to_cart'),
    url(r'^order-summary/$', order_details, name='order_summary'),
    url(r'^success/$', success, name='purchase_success'),
    url(r'^item/(?P<item_id>[-\w]+)/delete/$', delete_from_cart, name='delete_item'),
    url(r'^checkout/$', checkout, name='checkout'),
    #url(r'^payment/(?P<order_id>[-\w]+)/update/$', process_payment, name='process_payment'),
     url(r'^update-transaction/(?P<token>[-\w]+)/$', update_transaction_records,
        name='update_records')


    #url(r'^tip/create/$', views.TipCreate.as_view(), name='tip_create'),

]

1 个答案:

答案 0 :(得分:1)

是的,有可能。您无需覆盖Wagtail的serve-只需在urls.py行之前的顶级url(r'', include(wagtail_urls)) 中包含您的Django URL模式,即可使用它们Wa的全部模式。