views:

63

answers:

3

I have a template that has a button that posts just the button's name to a view which has the following code:

if request.POST:
    a = request.POST
    name = mymodel.objects.get(id = a )[0]
        return HttpResponse(name)

request.POST has the name of the button that submitted the post. THE error is: int() argument must be a string or a number, not 'QueryDict'

However, when I do: a = request.POST['name], django raises a no 'name' in post error. How do I fix this?

+3  A: 

This returns a QueryDict (similar to a python dictionary) with the data in it request.POST

anyway

request.POST['name']

should work. I would say theres no key 'name' in it. pearhaps it is called 'id_name' or something like that.

Have a look a the content of the query dict with a debugger or with a print statement:

print request.POST
maersu
I followed ur QueryDict link and in my view.py put in this : `return HttpResponse(a.items())`This printed out: (u'6', u'Delete').Delete is the name of the button and 6 is the value. How do I flip these so I can use request.POST['Delete']?
Ali
show us your form and template code
chefsmart
+1  A: 

Imagine the following code is in your template: -

<form>
    <button type="submit" name="option_one" value="option_one">Option One</button>
    <button type="submit" name="your_second_option" value="your_second_option">2nd Option</button>
</form>

You could do this in your view: -

def your_view(request):
    if request.POST.has_key('option_one'):
        #do your stuff
    if request.POST.has_key('your_second_option'):
        #do other stuff
chefsmart
A: 

The simplest way to do this is you use a hidden field. A button isnt really a good way to move data.

Ali