Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialandetin
5,263 PointsFiltering a model through forms - passing the selection [Solved]
I have a single Car model which I'd like to filter through interdependent ChoiceField's:
class Car(models.Model):
make = models.CharField(max_length=50)
model = models.CharField(max_length=50)
platform = models.CharField(max_length=50)
Forms:
class MakeSelectForm(forms.Form):
make = forms.ChoiceField()
def __init__(self, *args, **kwargs):
super(MakeSelectForm, self).__init__(*args, **kwargs)
qs = Car.objects.values_list('make',flat=True).distinct()
choices = [(value, value) for value in qs]
self.fields["make"].choices = choices
class ModelSelectForm(forms.Form):
model = forms.ChoiceField()
def __init__(self, *args, **kwargs):
make = kwargs.pop("make", None)
if not make:
raise ValueError("expected a 'make' keyword arg")
super(ModelSelectForm, self).__init__(*args, **kwargs)
qs = Car.objects.filter(make=make).values_list('model',flat=True).distinct()
choices = [(value, value) for value in qs]
self.fields["model"].choices = choices
Views:
def make_select_view(request):
make = None
if request.GET.get("submitted", None):
form = MakeSelectForm(request.GET)
if form.is_valid():
make = form.cleaned_data['make']
return redirect("reviews:model-select", make=make)
else:
form = MakeSelectForm()
return render(request, "reviews/makeselect.html", {"form": form, "make": make})
def model_select_view(request, make):
model = None
if request.GET.get("submitted", None):
form = ModelSelectForm(request.GET, make=make)
if form.is_valid():
model = form.cleaned_data['model']
else:
form = ModelSelectForm(make=make)
return render(request, "reviews/modelselect.html", {"form": form, "make": make, "model": model})
Makeselect.html:
{% extends "base.html" %}
{% block body %}
<form action="{% url 'reviews:model-select' make %}" method="GET">
<input type="hidden" name="submitted" value="1" />
{{ form.as_p }}
<input type="submit" value="Select" />
</form>
{% endblock %}
URL's:
urlpatterns = [
url(r'^$', views.make_select_view, name="make-select"),
url(r'^(?P<make>\w+)/$', views.model_select_view, name="model-select"),
url(r'^(?P<make>\w+)/(?P<model>\w+)/$', views.platform_select_view, name="platform-select"),
]
The problem is, I can't pass the selected "make" to the second view. The URL I get:
/None/?submitted=1&make=BMW
What's wrong with my make_select_view?
andetin
5,263 Pointsandetin
5,263 PointsOh, I just deleted:
{% url 'reviews:model-select' make %}
and it works!