1

I have a form that contains input tags that are hidden, when i POST the form to the views and print, i see the values i need but the content is not saving to the database here is my html

<form method="POST" action="/selly/cart/" item_id="{{product.pk}}" enctype="multipart/form-data">
    {% csrf_token %}
    <h1 name="description">Description is : {{each_item.description}}</h1>
    <p><input type="hidden" name="description" value="{{each_item.description}}"></p>

    <span name="price">Price is : $ {{each_item.price}}/piece</span>
    <p><input type="hidden" name="price" value ="{{each_item.price}}"></p>

    <p>Quantity is : <input type="number" default="0" name="quantity"> piece ( {{each_item.item_remaining}} pieces available )</p>
    <br>
    <input type="submit" class="btn btn-primary" value="Add to Cart">

</form>

here is my views.py

from selly.models import Cart
def cart(request):
    if request.method == "POST":
        print "rp ", request.POST

        description = request.POST['description']
        print "Description is ", description

        price = request.POST['price']
        print "Price is ", price

        quantity = request.POST['quantity']
        print "Quantity is ", quantity

        items =  Cart.objects.get_or_create(client="client", description="description", price="price", quantity="quantity")
        print "ITEMS", items
    return render(request, 'selly/cart.html', {'items': items})

Here is the model.py

class Cart(models.Model):
    description = models.CharField(max_length = 100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.IntegerField()

    def __str__(self):
        return self.description

    def total(self):
        return self.price * self.quantity

Is there a way of making it save to the database i created called Cart

6
  • Just call save method: items.save() Commented Mar 30, 2016 at 11:53
  • @Selcuk am getting [u"'price' value must be a decimal number."] as error Commented Mar 30, 2016 at 11:58
  • What price value are you entering? Commented Mar 30, 2016 at 12:01
  • He's assigning it as a string, see my answer. Commented Mar 30, 2016 at 12:02
  • not entering any value for price, if you see it it is in hidden input field. The only input field that is visible is quantity Commented Mar 30, 2016 at 12:03

1 Answer 1

1

I'm surprised you're not getting an error with that code - get_or_create returns a tuple, per the docs:

Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

So your line items = Cart.objects.get_or_create(client="client", description="description", price="price", quantity="quantity")

needs to be

items, created = = Cart.objects.get_or_create(client="client", description="description", price="price", quantity="quantity")

You'll also be able to interrogate that created variable, because if it's false, then it hasn't created a new object; if it hasn't created a new object, then all get_or_create is doing is returning the object already in the database.

You need to save the object manually if you're updating it.

This is because:

This is meant as a shortcut to boilerplatish code. For example:

try:
    obj = Person.objects.get(first_name='John', last_name='Lennon') 
except Person.DoesNotExist:
    obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    obj.save()

Full details are on the docs page for get_or_create.

Also, your last line is wrong - you're assigning strings to the object, where you do price="price" - you actually want to do price=price, because you're assigning object.price=price, which is what you called the variable in the code above. I'd suggest maybe calling those variables 'incoming_price' or similar to avoid shadowing/confusion.

Sign up to request clarification or add additional context in comments.

Comments

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.