0

I am currently working on an e-commerce web app and decided to use the Beginning Django Ecommerce book. I am following the content and implementing it in my own way but i am having some issues with some few functions that are not running. here are the apps with the files where i think the problem is coming from; 1. cart app models.py:

from django.db import models
from menu_items.models import Item
from smartmin.models import SmartModel
import django.db.models.options as options
options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('augment_quatity','name','price','get_absolute_url','total',)

class OrderItem(SmartModel):
    order_id = models.CharField(max_length=50)
    date_added = models.DateTimeField(auto_now_add=True)
    quantity = models.IntegerField(default=0)
    item = models.ManyToManyField(Item)

    class Meta:
        db_table='order_items'

        def __unicode__(self):
            return "%s" % (self.order_id)

        def total(self):
            return self.quatity *self.item.price
        def name(self):
            return self.item.name
        def price(self):
            return self.item.price
        def get_absolute_url(self):
            return self.item.get_absolute_url()
        # incase user orders same item twice we jus add on the quantity
        def augment_quatity(self, quantity):
            self.quatity = self.quantity + int(quantity)
            self.save

orders.py in the same app:

from cart.models import OrderItem
#from cart.models import order_id
from menu_items.models import Item
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
import decimal
import random

ORDER_ID_SESSION_KEY = 'order_id'

# get the current user's cart id, sets new one if blank
def _order_id(request):
    if request.session.get(ORDER_ID_SESSION_KEY,'') == '':
        request.session[ORDER_ID_SESSION_KEY] = _generate_cart_id
    return request.session[ORDER_ID_SESSION_KEY]
def _generate_cart_id():
    order_id =''
    characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()'
    order_id_length = 100
    for y in range(order_id_length):
        order_id += characters[random.randint(0,len(characters)-1
        )]
    return order_id
# return all items from the current user's order
def get_order_items(request):
    return OrderItem.objects.filter(order_id=_order_id(request))
# add an item to order
def add_to_order(request):  
    postdata = request.POST.copy()
    #get item slug from post data, return blank if empty
#   item_slug = postdata.get('item_slug','')
    #get quantity added, return 1 if empty
    quantity = postdata.get('quantity',1)
    # fetch the item or return  missing page error_message
    i = get_object_or_404(Item,)
    # get items in order
    order_items = get_order_items(request)
    item_in_orders = False
    # check to see if item is already in cart
    for order_item in order_items:
        if order_item.item.id == i.id:
            #update the quantity if found
            order_item.augment_quantity(quantity)
    item_in_order = True
    if not item_in_order:
        # creat and save a new order item
        oi = OrderItem()
        oi.item = i
        oi.quantity = quantity
        oi.order_id = _order_id(request)
        oi.save()

2.live app views.py

def show_order(request):
    if request.method == 'POST':
        postdata = request.POST.copy()
        if postdata['submit'] == 'Remove':
            order.remove_from_order(request)
        if postdata['submit'] == 'Update':
            order.update_order(request)
    order_items = order.get_order_items(request)
    page_title  = 'F4L order' 
    order_subtotal = order.order_subtotal(request)
    return render_to_response('public/order.html',context_instance=RequestContext(request))

Template where the functionality is not working,

{% extends "base.html" %}
{% block content %}
{% load menu_tags %}
<div style="height:30px">
 {% order_box request %}
</div>
<table summary="Your menu order" id="menu_order">
   <caption>Your F4L Orders</caption>
   <thead>
    <tr>
     <th scope="col">Item</th>
     <th scope="col">Price</th>
     <th scope="col" class="right">Total</th>
    </tr>
   </thead>
   <tfoot>
   <tr>
    <th class="right" colspan="2">
     Order Subtotal:
    </th>
    <th class="right">
    {{order_subtotal}}<span> frw</span>
    </th>
   </tr>
   {% if order_items %}
   <tr>
    <th class="right" colspan="2">
     <a href="/url/to/checkout/">Checkout Now</a>
    </th>
   </tr>
   {% endif %}
   </tfoot>
   <tbody>
    {% if order_items %}
     {% for item in order_items %}
    <tr>
     <td>

       {{ item.name }}

     </td>
     <td>{{ item.price }}<span> frw</span></td>
     <td class="right">
     <form method="post" action="." class="order">
     <label for="quantity">Quantity:</label>
     <input type="text" name="quantity" value="{{ item.quantity }}" id="quantity" size="2" class="quantity" max_length="5" />
     <input type="hidden" name="item_id" value="{{ item.id }}" />
     </td>
     <td>
     <input type="submit" name="submit" value="update"/>
     </form>
     </td>
     <td>
     <form method="post" action="." class="order">
     <input type="hidden" name="item_id" value="{{ item.id }}" />
     </form>
     </td>
     <td>
     <form method="post" action="." class="order">
     <input type="hidden" name="item_id" value="{{ item.id}}" />
     <input type="submit" name="submit" value="Remove" />
     </form>
     </td>
     <td class="right">{{ item.total }}<span> frw</span></td>
    </tr>
     {% endfor %}
     {% else %}
    <tr>
    <td colspan="2" style="height:30px;">
     Your F4L order is empty.
    </td>
    </tr>
    {% endif %}
   </tbody>
</table>
{% endblock %}

Now th problem is, the above template code is a page a user redirects to after submitting a form with the quantity of item he/she is buying but that is not happening. After submitting form with for example 10items,i redirect to this page(above _template_), it loads correctly but does not return the information i submitted.

i do understand that this is alot but i have really need your help and will appreciate any sort of help.

1 Answer 1

1

In show_order view you should pass your variables to template as dictionary:

...
context_dict = {'order_items': order_items, 'order_subtotal': order_subtotal}
return render_to_response('public/order.html', context_dict, context_instance=RequestContext(request))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks that fixes part of it, order subtotal returns a 0.00 value but the rest don't. so am thinking the problem is with the order_items variable
@sneawo dict is a built-in function in python, you should not use this word as a variable name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.