0

I use JqGrid in Django template. It works fine until adding new row to the grid. I use for this "+" button on navigation panel. The problem is when I click "Submit" on "Add Record" dialog the HTTP 403 error appears. If I change editurl to "/ajax/stocks/add" (remove last slash) the HTTP 500 error appears.

Does someone know what the problem can be here ? In browser, query to http://127.0.0.1:8000/ajax/stocks/add/ (django dev server used) returns True as expected.

And I don't understand completely what ajax_stock_add_view function should do. Should it return success status or something else back?

Here is my django template:

 {% block extrascript %}
    <script type="text/javascript">
        $(document).ready(function() {
            jQuery("#stocks-grid").jqGrid(
                    { url:'/ajax/stocks/', datatype: "json", colNames:['Name','Comment'],
                        colModel:[
                            {name:'name',index:'name', editable:true},
                            {name:'comment',index:'comment', editable:true}
                        ],

                        editurl : "/ajax/stocks/add/", rowNum:10, pager: '#stocks-grid-pager', sortname: 'name', viewrecords: true});
            jQuery("#stocks-grid").jqGrid('navGrid', '#stocks-grid-pager');
        });
    </script> {% endblock %}

{% block content %}
    <table id="stocks-grid"></table>
    <div id="stocks-grid-pager"></div> {% endblock %}

Here is urls.py:

...
     (r'^ajax/stocks/$', ajax_stocks_view),
     (r'^ajax/stocks/add/$', ajax_stocks_add_view),
...

and views.py

def ajax_stocks_view(request):
    json = get_grid_json(Stock)
    return HttpResponse(json, mimetype='application/json')

def ajax_stocks_add_view(request):
    return HttpResponse(True)
mt_serg
  • 7,487
  • 4
  • 29
  • 45

2 Answers2

1

It depends on how exactly you're trying to set this up. If you have a form to create a new item and when the user submits you want the new item to be saved to the database and appended to the grid, then here's your process:

  1. User submits form
  2. Form is sent to server via AJAX request to /ajax/stocks/add/
  3. View at /ajax/stocks/add/ adds item to database and returns a response (You must send some kind of actual response. True is not a response, but '[{"success":true}]' (JSON) is, with proper mime-type.)
  4. Client receives response from view at /ajax/stocks/add/. If request was successful, JavaScript appends to the grid, otherwise error is display to user.
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for the explanation. In terms of JqGrid I should use afterSubmit event. I'just tried it, but the event doesn't call. It's because of the HTTP errors that I don't solve yet. – mt_serg Jun 23 '11 at 16:06
0

The problem was around CSRF protection in Django. In my case JqGrid created a form without the CSRF token, so CSRF verification was failed. Here is a good solution for the problem.

Community
  • 1
  • 1
mt_serg
  • 7,487
  • 4
  • 29
  • 45