views:

31

answers:

1

Hello. I wonder if you could help me.

I have a list of data that will be displayed on one page. There is a simple search box, a list of categories and a list of tags that can all be used to filter the list of data. I'm trying to built it from the ground up (so it doesn't require JavaScript) but eventually it will submit the search criteria and return back a new list using Ajax.

So I have a list of categories in my database ('large', 'small', etc), and I have a list of tags in my database ('wooden', 'brass'). Tags are used to filter down more of what's in the categories. I then have a search box. Ideally, I want the user to effectively tick which categories they want, tick what tags they want and possibly put a search for keywords, and then submit all of that data so it can be queried and a new list of the filtered data can be returned.

I'm not a Django expert, and I'm stuck on how and where to do this... What is the Django way of spitting out the categories as a checkbox list, the tags as a checkbox list and the search box with a submit button... Which when submitted, I can take all that data and do the necessary queries on the database? I don't quite understand how I'd do this... I've been looking at the Django Docs and the Django Book for a few days and the way I'm doing things doesn't seem to be listed.

Please, any help at all would be fantastic.

+1  A: 

spitting out the categories as a checkbox list,

the tags as a checkbox list and the

search box with a submit button...

This is a <form> in your HTML page. It probably doesn't match anything in the Django model very well. It's a unique form built more-or-less manually.

I can take all that data and do the necessary queries on the database?

That's a view function.

You'll probably have something like this.

objects= SomeModel.objects
if request.GET ... has categories ...
    objects = objects.filter( ... categories ... )
if request.GET ... has tags ...
    objects = objects.filter( ... tags ... )
if request.GET ... has search ...
    objects = objects.filter( something__contains( search ) )
return render_to_response( ... etc. ... )

the way I'm doing things doesn't seem to be listed.

You're beyond the tutorial here.

What to do?

  1. Do the ENTIRE tutorial. All the way through. Every step. It doesn't seem like it solves your problem, but you MUST do the ENTIRE tutorial.

  2. Design your model. You didn't mention the model in the question. It's the absolutely most important and fundamental thing.

  3. Create the default admin interface for that model. Get the default admin interface to work and do the kinds of things you'd like to do. It has great search, category and tag filtering.

    In order to get the default admin to work, you'll need to design fairly sophisticated model and form features. You'll probably have to add method functions to your model as well as choice items and other goodness.

  4. AFTER you have the admin page pretty close to what you want, you can write you own customized view.


each single checkbox has a different name ('category_option_1', 'category_option_2', etc.) ... How do I read these? I can't just put request.POST['category_option_n']?

Really? Why didn't your question say that?

Are you asking about this?

for k in range(1024):
    name = 'category_option_{0}'.format(k)
    # Use request.POST.get(name,None) to build a `Q` object
S.Lott
@s-lott Thanks for the reply. I'm thinking I will need to do this manually. I have all my stuff setup correctly in the admin... It's the user side that's the problem. Because I'm using checkboxes (so multiple categories or tags can be selected) and each single checkbox has a different name ('category_option_1', 'category_option_2', etc) that's where I'm stuck in the view part... How do I read these? I can't just put request.POST['category_option_n']? I'll need a POST check for every checkbox on the page and that doesn't seem very good.
littlejim84
@littlejim84: "that's where I'm stuck in the view part". Then actually say this in your question. Don't hope we guess that this is where you're stuck.
S.Lott
@s-lott Sorry looks like I offended you. After you gave your answer, I followed that and realised then I was stuck on the bit I commented on. Your answer was helpful to start off with, got the ball rolling, then I needed to refine it further. Thanks.
littlejim84
@littlejim84: (1) I'm not offended. (2) Updating your question is far, far more important than apologizing. Please actually update your question so it is correct and complete. Other people have the same problem.
S.Lott