I have a Django form. In which there are two select fields from several values. The data from them is contained in the Django model database.
The first field of the form contains car brands.
In the second field of the form, I would like to display car models - depending on the car brand selected above.
How can I make the data appear in the second field of the form depending on the selected value of the first field?
class Model_for_form(models.Model):
name_1 = models.CharField(max_length=150, verbose_name="Name_1")
name_2 = models.CharField(max_length=150, verbose_name="Name_2")
def __str__(self):
return self.name_1, self.name_2
class Form_1(forms.ModelForm):
class Meta:
model = Model_for_form
fields = "__all__"
def form_1(request):
context = {}
form = Model_for_form(request.POST or None)
if form.is_valid():
form.save()
return redirect("form_0")
context['form_1'] = form
return render(request, "form_1.html", context)
{% extends "base.html" %}
{% block content %}
<hr/>
<div class="row">
<div class="col-6">
<form method="POST" class="post-form">
{% csrf_token %} {{form_1.as_p}}
<button type="submit" class="save btn btn-light">button</button>
</form>
</div>
</div>
<br/>