views:

488

answers:

2

I have spent too much time on this problem and am beginning to think that it can't be done in Django. I am pulling a list of pathogen names from a postgres database to a drop down box. The user selects the pathogen, which requires the id to be passed back through django to the database for further retrieval of more data. Please see

http://dpaste.com/119358/ for the drop down code.

The problem is that the select id = statement is above the point where the form retrieves the pathology id. The django statements must be placed at this point or the drop down doesn't work. Has anyone written something similar and found a solution?

Max

+5  A: 

The id of the select tag in your HTML does not need to have any knowledge of your pathology records. Your code is correct, except for the lack of closing select tag.

Your Django view that is mapped to the /pathology/ action URL needs to handle the POST method and pull the pathology id out of the request like so:

pathology_id = request.POST['pathology']

Now you can look that object up in your database by its id and generate a new page/form or whatever business logic you need to do with it.

The dictionary key that you need to use in the request.POST['...'] call is based on the name you've given your select element, in this case 'pathology'.

Joe Holloway
pathology() takes exactly 2 arguments (1 given) is the error I get, which seems to say the variable is not getting passed through. I do have the pathology_id=request.POST['pathology'] in the view. I do have the ending </select>, now, as well. Max
The selected pathology id is not going to be passed to your view as an argument, but rather as POST data attached to the 'request' object. The function signature for your view should be something like this: def pathology (request): ...
Joe Holloway
Thank you!! Thank you!! Thank you!!! I finally have all the little pieces working together. Yes, I realize now def pathology(request) and not def pathology(request,pathogen). Sorry, I've been such a pest with this problem, but I'm panicking to get something up and running and I'm new at Django.
No worries, glad to help. Please use the check mark next to the up/down vote buttons to denote "accepted solution". Doing so will give reputation points and will also help make sure you get answers to your questions in the future.
Joe Holloway
A: 

Django's form handling library is a great tool for managing your HTML forms. It will pull the options from the database, generate the HTML code for the drop down box and save the selection into the database for you with minimal code.

akaihola