0

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:

  1. book genre
  2. city
  3. age

Redirected page has three tables:

  1. Most read books in this genre in this city by people around this age
  2. List of libraries in this city, sorted based on how many books in this genre are there
  3. 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.

  • Reading form selections and using them to search a database and display results is a standard pattern in Django. I recommend completing a a Django tutorial - nearly every Django tutorial will cover this. – jastr Jan 06 '19 at 19:43
  • You'll need to create a view/method that the form gets sent to. In that view, you can access the form https://stackoverflow.com/questions/4706255/how-to-get-value-from-form-field-in-django-framework – jastr Jan 06 '19 at 19:44
  • @jastr thank you for responding! I've read official documentation about the select widget and I did go through several posts about adding a post method in views, however couldn't find how to redirect to another template and keep the selected choices. I was also confused by how it should look like on the database side since I've never worked with databases before, but I guess I will just keep searching. "if form.is_valid()" is never true with this code for some reason. – tardis314 Jan 06 '19 at 20:47
  • A django tutorial (not just documentation) will show you how the pieces fit together. Also, instead of using a class based view, like TemplateView, you can write a regular view which you might find easier to manipulate. – jastr Jan 08 '19 at 02:26

0 Answers0