Django form that varies according to another form

Asked

Viewed 38 times

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.

  • 1

    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.

  • No Javascript can not do?

  • 1

    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

  • Share it with me, the goal is to learn the Jango framework well. I’m pushing javascript with my stomach for now.

  • 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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.