简体   繁体   中英

How can i save a form without a submit button in django

i have a doubt, i want to save a form and without use a submit button.

Because, as you´ll see:

在此处输入图片说明

i need to increase "cantidad" but i don't want to use a "sumbmit" button, otherwise, i need that the data (integers) will stay correctly in a database field.

Because, in my models.py i have:

cantidad = models.IntegerField(default=0)

I want to modify the field 'cantidad' when the user increase or decrease Selection, as you see in the image and without a submit button.

Is there a way to do this?

Any help thank you!.

After talking to you, something like this will work, but you'll have to modify it to your needs. I would use sessions because their values are stored without having to save your current modelfield in the database, and users could still browse the site without having to update the values every time.

Remember: Sessions are accessible like dictionaries. But you have to .save() them just like you would any model, because it's still a model stored in the database. The reason you should use these instead of your product model is because sessions are intended to be random, and are easy to clear with python manage.py clearsessions . If you used your own model, assuming you have thousands of models to dig through which were never purchased by someone, this is FAR EASIER and faster, and keeps your database cleaner.

This is a quick answer, though I suggest you implement a shopping cart that's already been made by someone else. Check djangopackages.org for some good ones. What they have will be better than what I'm about to show you, though this is the principle you follow either way.

GENERAL STEPS TO TAKE:

  1. Make a session key: request.session['cart'] = {}
  2. Add keys and values to it: request.session['cart']['product_id'] = 111
  3. Add more values to that product: request.session['cart']['quantity'] = 222
  4. Save it: request.session.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM