2

I am attempting to create a promo code for a shopping cart that I already have. I want it to be simple, such as typing 100off to get $100 off, I am getting a error saying "global name 'PromoCode' is not defined".

models.py

class PromoCode(ModelForm):
        code = models.FloatField(max_length=15)
        discount = models.FloatField(max_length=15)

views.py

def addtocart(request, prod_id):
        if (request.method == 'POST'):
                form = CartForm(request.POST)
                if form.is_valid():
                        newComment = form.save()
                        newComment.session = request.session.session_key[:20]
                        newComment.save()
                        return HttpResponseRedirect('/products/' + str(newComment.product.id))
        else:
                form = CartForm( {'name':'Your Name', 'session':'message', 'product':prod_id} )

        return render_to_response('Products/comment.html', {'form': form, 'prod_id': prod_id})

def delItem(request, prod_id):
        addtocart = get_object_or_404(Cart, pk = prod_id)
        prod_id = addtocart.product.id
        addtocart.delete()
        return HttpResponseRedirect('/userHistory/')


    def userHistory(request):
            promo = PromoCode.objects.filter(code = code_from_the_form)
            userCart = Cart.objects.filter(session = request.session.session_key[:20])
            totalCost = 0
            for item in userCart:
                    print item
                    totalCost += item.quantity * item.product.prodPrice * 1.06
            return render_to_response('Products/history.html', {'userCart':userCart, 'totalCost' : totalCost})
Ryan Tice
  • 731
  • 3
  • 13
  • 16

1 Answers1

0

Add a PromoCode model with two fields: code and discount. You can then add a couple of promo codes in the admin.

In the form, just add a promo code field and upon submit, check if the code matches any of your PromoCode objects and apply the discount.

(And perhaps a bit of javascript to check the code on the fly. And I'd add some checks here and there to make sure your discount is well between 0 and 1 ("0.5") if you just want to multiply and well between 0 and 100 if it is a percentage. Just make sure you cannot make a mistake with it, that would be my fear if I'd have to implement it :-)

Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
  • I created the model, but I am unaware of where to go from here, I am not terribly worried about checking the code as this is for a basic ecommerce site that is for a class, thanks again for your help. I added the new model aboove, is that correct? I apologize I am still new to developing. – Ryan Tice Apr 25 '12 at 14:47
  • I'd turn the discount into a floatfield (or integerfield if you go for percentages). You probably have a django view with a form for showing the shopping card and reacting to add/delete item events? Add an extra field there ("promo code") and do a PromoCode.objects.filter(code=code_from_the_form) to see if there's some promotion discount you have to apply. – Reinout van Rees Apr 25 '12 at 18:54
  • I am getting an error global name 'PromoCode' is not defined, I posted my models and views above, thanks for taking the time – Ryan Tice Apr 25 '12 at 19:08
  • (a) you must explicitly import PromoCode in views.py from models.py. The variable doesn't just spring to life :-) (b) Could you restore your original question somewhat? A stackoverflow question is supposed to be a proper generic question, not a place to copy/paste code. – Reinout van Rees Apr 25 '12 at 19:48
  • Okay, I redid the question, and imported the PromoCode model, however now I receive type object 'PromoCode' has no attribute 'objects' – Ryan Tice Apr 25 '12 at 23:10
  • What I meant was "restore the original question", not "make sure my (more or less correct) answer isn't an answer to the question anymore". You're asking a completely new question on a programming detail now instead of a question that's useful to others. The original question was reasonably OK, but the current one will just get closed, I think. – Reinout van Rees Apr 26 '12 at 08:41
  • Regarding the code: only a model has a .objects, but you inherit from a modelform instead of a model. Best to go to the Django website and read the explanation pages on models and forms, I think. – Reinout van Rees Apr 26 '12 at 08:43
  • I looked over the form portion but did not receive the help I needed, do you have a more specific link or tip? – Ryan Tice Apr 27 '12 at 04:29
  • @RyanTice you don't want to look at the forms documentation. Read the documentation on Models. Better yet, give yourself a few hours and read through the beginning tutorials: https://docs.djangoproject.com/en/dev/intro/tutorial01/ – Jordan Reiter Sep 12 '12 at 19:13