0
How do I create a form in Jango of an object and that the price of the object varies according to the size of the object without having to update the page.
#py.models
class Pizza(models.Model):
SIZE_OPTIONS = (
('30', 'Pequena'),
('35', 'Média'),
('40', 'Grande'),
id = models.AutoField(primary_key=True)
flavor = models.ForeignKey(Flavor, default=None, on_delete=models.CASCADE)
size = models.CharField(max_length=2, choices=SIZE_OPTIONS)
#form py.
class OrderForm(forms.ModelForm):
class Meta:
model = Pizza
fields = ['flavor', 'size']
#html template.
{% extends 'base.html' %}
{% block content %}
{{ form.as_ul }}
{{ price }}
{% endblock %}
I want the person to select the size and price upgrade alone.
Ideally you will have to do with Javascript so that the user has one feedback... And later, by submitting the form, Django would calculate this price again.
– fernandosavio
No Javascript can not do?
– Thomas Caio
Without Javascript you can only communicate with the server by sending the form... Unless you think of some gambiarra, but I do not advise. I can think of a few, but I’d rather not outsource for your own good... D
– fernandosavio
Share it with me, the goal is to learn the Jango framework well. I’m pushing javascript with my stomach for now.
– Thomas Caio
What you could do is list all combinations of flavor and size with the prices and then validate everything on
clean
of the form. But like I said, you’d better calculate this with Javascript just to show to the client, and then your server calculates again to save to the database.– fernandosavio