Re: Getting error 500 when using jquery/ajax

1,260 views
Skip to first unread message

Mario Gudelj

unread,
Jan 18, 2013, 6:26:08 PM1/18/13
to django...@googlegroups.com

Replicate this error in chrome, then go to network tab in dev tools, look at the request that gets 500 and check the response. You should be able to see the stack trace if debug is on.

On 19 Jan, 2013 8:01 AM, "Dex" <dexte...@gmail.com> wrote:
Hi, I'm new to Django. I'm creating a game that has a 10x10 board and when a cell is clicked upon, it will be marked with an "X". Marking the cells works in jquery but I want to send data to server side to let it know that the cell is marked. That way when I quit the  game and come back later, it will have an "X" on that cell. Currenly, my jquery code is this:
    $('.target_cell').click(function(){
            if ($(this).text() != "X" && $(this).text() != "H"){
                $(this).text("X");
                var spot = $(this).attr('name'); //Cell index number like in an array but in string format
                $.ajax({
                    type: "POST",
                    url: "/target_spot/{{game.id}}/" + spot + "/",
                    data: spot
                });
            }

And my view code is this:
    @csrf_exempt
    def target_spot(request, game_id, spot):
        if request.is_ajax():
            game = fetch_game(request.user, game_id)
            game.creator_target_board[int(spot)] = "X"
            game.save()

From my server log, i get the following message when I click on a cell: "POST /target_spot/1/0/ HTTP/1.1" 500. Any ideas on how to solve this? Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Aux0I4-eUNMJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Iñigo Medina

unread,
Jan 19, 2013, 4:04:54 AM1/19/13
to django...@googlegroups.com

On Fri, Jan 18, 2013 at 12:50:26PM -0800, Dex wrote:
> From my server log, i get the following message when I click on a cell:
> "POST /target_spot/1/0/ HTTP/1.1" 500. Any ideas on how to solve this?
> Thanks!

Don't you have debug enabled? It should give you a more complete track of the
error.

I�igo

Raphael

unread,
Jan 19, 2013, 5:35:57 AM1/19/13
to Dex, django...@googlegroups.com
hello Dex,

you did not tell us what browser you are testing your AJAX request on.
Maybe you experience this Chrome behavior:
http://develissimo.com/blog/2013/ajax-not-working-chrome-ie8/

Good luck
>Raphael

Bill Freeman

unread,
Jan 19, 2013, 8:05:06 AM1/19/13
to django...@googlegroups.com
What happens if you type the url in directly in the browser's location bar (you will
have to disable the request.is_ajax() test temporarily)?  If the server is in debug
mode, you should get a stack trace that is more informative.

Also, assuming that you've shown us all of the view code, you may need to
return something other than None (which is what gets returned if you don't
use the return statement).  (But I'm not sure of that.  I've always had something
to return.)

Bill

--
You received this message because you are subscribed to the Google Groups "Django users" group.

Raphael

unread,
Jan 19, 2013, 10:16:54 AM1/19/13
to django...@googlegroups.com
Hello Dex,

forget my story about Chrome/Chromium issue if you have been testing with FF.
This was a first shot into the dark. sorry.

Concerning your error message:
'unicode' object does not support item assignment

This error pops up when you try to alter immutable string objects.

Please check your code if you do so and tell us.
+add traceback and view code.

Thank you
--
Raphael
http://develissimo.com


Raphael

unread,
Jan 19, 2013, 10:29:48 AM1/19/13
to django...@googlegroups.com

game.creator_target_board[int(spot)] = "X"

Exception Type: TypeError at /target_spot/1/0/
Exception Value: 'unicode' object does not support item assignment

So your trouble is that game.creator_target_board[int(spot)] is an immutable str object.
so you have to rethink your strategy.

create new str object or handle it completely different.

--
Raphael
http://develissimo.com


Dex

unread,
Jan 19, 2013, 10:32:30 AM1/19/13
to django...@googlegroups.com
Raphael,

Seems like the problem might be in my models. My view code that tries to access the model is:
@csrf_exempt
def target_spot(request, game_id, spot):
    game = fetch_game(request.user, game_id)
    game.creator_target_board[int(spot)] = "X"
    return HttpResponse('/')

game.creator_target_board[int(spot)] = "X" tries to change a unicode object. creator_target_board is an attribute of my game object. In my model, creator_target_board = models.CharField(max_length=100, default=" " * 100). I was trying to replicate an array but since I couldn't figure out what type of field to use for an array I decided to use a CharField with a default value of " " * 100. The "spot" variable in my javascript is supposed to point to the position of creator_target_board to be modified.

Raphael

unread,
Jan 19, 2013, 10:57:53 AM1/19/13
to Dex, django...@googlegroups.com
@ Dex,

So you want to store the actual game state in the db to recover game-session-data after a period of time
and/or to perform game logic on the server side.

well I am sure you already know it, but there are approaches to handle array fields within the django ORM.
example: http://www.djangopackages.com/grids/g/arrayfield/landscape/
or: https://github.com/ecometrica/django-dbarray

but I have never tried one of them.
could be of interest one day!

I am interested in your final solution. Maybe you can send me an email once you decided a way to go.

thx

--
Raphael
http://develissimo.com


Message has been deleted

Dex

unread,
Jan 19, 2013, 2:54:50 PM1/19/13
to django...@googlegroups.com, Dex
Raphael,

Exactly what you said. I'm trying to build a "Battleship" game app with Django, and I want to be able save the game state in the db so I can revisit it later if I quit the game before it is finished.

Thanks for the links provided. I'm new to Django and still fairly new to programming overall, so I don't really know a lot about array handling with Django's ORM. Currently, I'm considering using a custom field to handle my array but I'm not really sure on this approach yet. I'll certainly let you know when I finish this project :)
Reply all
Reply to author
Forward
0 new messages