Get 500 error with Ajax on firebug

370 views
Skip to first unread message

Tsung-Hsien

unread,
Nov 20, 2011, 5:32:05 AM11/20/11
to Django users
The error does not show on Django, but firebug shows the error.
I refer "Django 1.0 Web Site Development" and use the jQuery code
below:


function bookmark_save() {
var item = $(this).parent();
var data = {
url: item.find("#id_url").val(),
title: item.find("#id_title").val(),
tags: item.find("#id_tags").val(),
};
$.post("/save/?ajax", data, function (result) {
if (result != "failure") {
item.before($("li", result).get(0));
item.remove();
$("ul.bookmarks .edit").click(bookmark_edit);
}
else {
alert("Failed to validate bookmark before
saving.");
}
});
return false;
}

First, I encountered 403 error and added javascript code of official
document to solve the problem.
After 403 error, the 500 error shows on firebug as "NetworkError: 500
INTERNAL SERVER ERROR - http://127.0.0.1:8000/save/?ajax".
The data can be updated, but the code as below doesn't action.
if (result != "failure") {
item.before($("li", result).get(0));
item.remove();
$("ul.bookmarks .edit").click(bookmark_edit);
}
else {
alert("Failed to validate bookmark before
saving.");
}

How to solve this?
Thanks!!

Tsung-Hsien

unread,
Nov 20, 2011, 6:17:51 AM11/20/11
to Django users
I type wrong in view.py, causing the problem.
Have been solve this.

On Nov 20, 2:32 am, Tsung-Hsien <jasoniem9...@gmail.com> wrote:
> The error does not show on Django, but firebug shows the error.
> I refer "Django 1.0 Web Site Development" and use the jQuery code
> below:
>
> function bookmark_save() {
>         var item = $(this).parent();
>         var data = {
>                 url: item.find("#id_url").val(),
>                 title: item.find("#id_title").val(),
>                 tags: item.find("#id_tags").val(),
>         };
>         $.post("/save/?ajax", data, function (result) {
>                 if (result != "failure") {
>                         item.before($("li", result).get(0));
>                         item.remove();
>                         $("ul.bookmarks .edit").click(bookmark_edit);
>                 }
>                 else {
>                         alert("Failed to validate bookmark before
> saving.");
>                 }
>         });
>         return false;
>
> }
>
> First, I encountered 403 error and added javascript code of official
> document to solve the problem.
> After 403 error,  the 500 error shows on firebug as "NetworkError: 500

> INTERNAL SERVER ERROR -http://127.0.0.1:8000/save/?ajax".

Mike Duke Hall

unread,
Dec 21, 2011, 11:07:41 AM12/21/11
to django...@googlegroups.com
Tsung,  what was the problem in your views.py?
I am working on this same problem.  Here is my views.py
------------------------------------------------------------------------------------- 
@login_required
def bookmark_save_page(request):
    ajax = 'ajax' in request.GET
    if request.method == 'POST':
        form = BookmarkSaveForm(request.POST)
        if form.is_valid():
            bookmark = _bookmark_save(form)
            if ajax:
                variables = RequestContext (request,{
                    'bookmarks':[bookmark],
                    'show_edit': True,
                    'show_tags': True
                })
                return render_to_response('bookmark_list.html',variables)
            else:
                return HttpResponseRedirect('/user/%s' % request.user.username)
        else:
            if ajax:
                return HttpResponse(u'failure')
    elif 'url' in request.GET:
        url = request.GET['url']
        title = ''
        tags = ''
        try:
            link = Link.objects.get(url=url)
            bookmark = Bookmark.objects.get(link=link,user=request.user)
            title = bookmark.title
            tags = ' '.join(tag.name for tag in bookmark.tag_set.all())
        except (Link.DoesNotExist, Bookmark.DoesNotExist):
            pass
        form = BookmarkSaveForm({
            'url':url,
            'title':title,
            'tags':tags
        })
    else:
        form = BookmarkSaveForm()
    variables = RequestContext (request,{
        'form':form
    })
    if ajax:
        return render_to_response('bookmark_save_form.html',variables)
    else:
        return render_to_response('bookmark_save.html',variables)
------------------------------------------------------------------------------------- 

Help from anyone is appreciated, I've been stuck on this too long.
Reply all
Reply to author
Forward
0 new messages