I am very new to Django, to Stackoverflow and to coding in general, so I'd appreciate any help.
I am trying to add a form to my website, which would have only select fields. After a user selects all options, I want to redirect them to another page, where I return different information in several tables from my database, based on all selects (all selected options together influence information shown in the tables).
I know this example is awkward, this is all made up just to give an idea of what I want to achieve in the end.
An example: First page has 3 options:
- book genre
- city
- age
Redirected page has three tables:
- Most read books in this genre in this city by people around this age
- List of libraries in this city, sorted based on how many books in this genre are there
- How to sign up to top 3 libraries
A user does not modify the database in any way, so I suppose the form can have GET method.
So my question is what would be the best way to get values from the user and get a unique value based on that from the database? I want to return one list for each table, which I am planning to be regularly updating for each city, for each age group and for each genre.
I am trying to do this with the select widget now. For now I do not return anything, because I am not sure how to get the data from the user and use it.
forms.py:
class MyForm(forms.Form):
select1 = forms.ChoiceField(widget=forms.Select,
choices=Select1.objects.all().values_list('id', 'name'))
select2 = forms.ChoiceField(widget=forms.Select,
choices=select2.objects.all().values_list('id', 'name'))
select3 = forms.ChoiceField(widget=forms.Select,
choices=select3.objects.all().values_list('id', 'name'))
views.py
class Page(TemplateView):
template_name = 'project/index.html'
def get(self, request):
form = MyForm()
return render(request, self.template_name, {'form': form})
html:
<select name="{{ form.select1.name }}">
<option class="dropdown-menu" value="" disabled selected>Please
select</option>
{% for choice in form.select1.field.choices %}
<option value="{{ select1.0 }}">{{ select1.1 }}</option>
{% endfor %}
</select>
and the same code for the other two selects. I rendered them separately because of the way I designed the website.