How to get json using django and jquery ajax? got 500 Intenal server error
Question: I'm trying to show dynamicly details of a certain object, getting them from a
sqlite3 database. I based my code on a tutorial, everything is exactly the
same, but I get a 500 Internal Server Error on my page (but the tutorial runs
perfectly).

I have python 3.3 and django 1.6 installed.

Here's my code:

url.py :

    
    
    url(r'^cargar-inmueble/(?P<id>\d+)$', 'inmobiliaria.views.cargar_inmueble', name='cargar_inmueble_view'),
    

views.py :

    
    
    import json
    from django.http import HttpResponse, Http404
    from inmobiliaria.models import *
    
    ....
    
    def cargar_inmueble(request, id):
        if request.is_ajax():
            inmueble = Inmueble.objects.get(id=id)
            return HttpResponse( json.dumps({'nombre': inmueble.nombre,
            'descripcion': inmueble.descripcion, 'foto' : inmueble.foto }),
            content_type='application/json; charset=utf8')
        else:
            raise Http404
    

hover.js (it's the main js script, have to rename it)

    
    
    $(document).on("ready", inicio );
    
    function inicio() {
    
        ...
    
        $("#slider ul li.inmueble").on("click", "a", cargar_inmueble);
    }
    
    function cargar_inmueble(data) {
    
        var id = $(data.currentTarget).data('id');
    
        $.get('cargar-inmueble/' + id, ver_inmueble);
    
    }
    

Looking at the console of the chrome dev tools, everytime I click the link
that calls "cargar_inmueble", I get this
[error](http://i.stack.imgur.com/aZyup.png) and "ver_inmueble" is never
called.. It's my first web site using python so I'm pretty lost!


Answer: Check network tab of chrome dev tools then you will know the source of the
problem.

Another way to debug is to simplify your view:

    
    
    def cargar_inmueble(request, id):
        inmueble = Inmueble.objects.get(id=id)
        return HttpResponse( json.dumps({'nombre': inmueble.nombre,
            'descripcion': inmueble.descripcion, 'foto' : inmueble.foto }),
            content_type='application/json; charset=utf8')
    

Then go to `http://localhost:8000/cargar-inmueble/1` directly and you will see
the stack trace if you leave `DEBUG=True` in `settings.py`.

Most likely this line can cause the error:

    
    
    inmueble = Inmueble.objects.get(id=id)
    

When `id` doesn't exist, the it will throw DoesNotExist exception, you should
catch it. Also I believe returning JSON is a bit different to what you are
doing:

    
    
    def cargar_inmueble(request, id):
        try:
            inmueble = Inmueble.objects.get(id=id)
        except Inmueble.DoesNotExist: # catch the exception
            inmueble = None
    
        if inmueble:
            json_resp = json.dumps({
                'nombre': inmueble.nombre,
                'descripcion': inmueble.descripcion, 
                'foto' : inmueble.foto 
            })
        else:
           json_resp = 'null'
    
        return HttpResponse(json_resp, mimetype='application/json')
    

Of course you can use
[`get_object_or_404`](https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-
object-or-404) for simpler code. I just wanna show the basic idea.

Hope it helps.

