Monday, October 23, 2017

Building a search form with 4 required choices, which will be inserted as arguments in an URL [warning: long question]

This is a semi-long question.  Please let me know wherever I make a mistake.

I'm building a search form with 4 required choices, 1 of the choices is a CharField with a max_length of 3.  The other 3 choices are ChoiceField's.  Here is a picture of what the search form looks like:


I've read through the Django docs and other things on the Internet but I'm still very unclear about how to approach this.

1. First I believe I need to create 4 args or kwargs in the url for each of the choices.  If I use args, it would look something like...

url(r'^search?(\w+{3})&(\w+{2})&(\w+{1-2})&(\w+{1-2})', function/class view, name='search')

2. Next, I need to pass the user's input in the html page into the url args.  I have a form set up to get the user's input.  But how would I write this on the HTML page?  Do I simply make a url tag like {% url 'search' args1, args2, args3, args4 %}?

3. Then I need to create the views function.  My first question is, should I use a function-based view or the generic class-based ListView?  The result of the search will be a list of items, so ListView is a good template to use, but I don't know how having 4 args would affect using ListView.  My second question is, what is the most effective way of making this search?  I need to check if any records in the database matches the user's search, so would I do something like 'For record in database' to check over every record in the database?

If I used a function view, would it look something like:

def search(request, args1, args2, args3, args4):
     for record in database:
          if args matches record:
               display record

Thank you for reading through the question

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/01912c3f-f10b-4134-8731-908b6585d6c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment