On Friday, February 4, 2011 1:41:36 AM UTC, mf wrote:
>
> I want to have a little feedback form in one of my views but I can't 
> get it to work. I put a pdb.set_trace() to debug the views but when I 
> click submit the view reloads and never gets into the if 
> request.method == 'POST' and... code section. I'm very confused about 
> how to approach ajax forms submission with django. When I want to make 
> somthing like update a db value clicking in a button(i.e: votes) I can 
> create another view for handling the ajax call, but in this case I've 
> to generate the empty form in the original view, so I guess I can't 
> create another one to handle the ajax call. 
>
> views.py: 
>
> def results(request, query): 
>     # View code outside the form 
>     ipcs_selected = request.session['ipcs_requested'] 
>     query2 = ' | '.join(query.split()) 
>     base_query = '@title (%s)' % (query2) 
>
>     # code that I want to run when the user submit's the form 
>     results = {'success': False } 
>     if request.method == u'POST' and request.is_ajax(): 
>         form = FeedBackForm(request.POST) 
>         POST = request.POST 
>         # work with the form 
>         if form.is_valid(): 
>             results = {'success':True} 
>         json = simplejson.dumps(results) 
>         return HttpResponse(json, mimetype='application/json') 
>     else: 
>         form = FeedBackForm() 
>
>
>     return render_to_response('results.html', 
>                 {'patents': patents, 'query': query, 'bq': base_query, 
> 'form': 
>                 form}, 
>                 context_instance=RequestContext(request)) 
>
> results.html: 
>
> <script> 
>         function submit_form() { 
>             $.post("/results/{{query}}/", { query:{{ query }}, full: 
> {{full_query }} }, function(json){ 
>                 alert("Success?: " + json['success']); 
>             }); 
>         } 
>         function addClickHandlers() { 
>             $("#submit").click( function() { submit_form() }); 
>         } 
>         $(document).ready(addClickHandlers); 
>   </script> 
>
>   <form action="" method="post" id="form"> 
>       <p>Relevant results?</p> 
>       {{ form.relevance }} 
>       <p>Up to which page?</p> 
>       {{ form.rel_pages }} 
>       <input type="submit" name="submit" value="SUBMIT" id="submit" /> 
>   </form> 
>

Couple of issues here.

Do you really want the entire 'query' variable inside your URL? That seems 
weird, especially as you're also submitting it as one of the POST variables.

But the main problem is that your JS submit function doesn't return false, 
so it doesn't cancel the default submission action: so your JS starts to 
run, then the form submits via the normal HTTP POST, and the page is 
refreshed.

        function addClickHandlers() { 
            $("#submit").click( function() { 
                submit_form();
                return false;
            }); 
        } 
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to