I'm trying to enter a word and have it show up on page through ajax. There something simple I'm missing...
So I'm sending the info like this with Jquery:
$.ajax({
url: url,
type:"POST",
data:{'word': word},
success: function(data){
//do something
}
});
and the information is getting into the view and saving into the DB. The problem happens when I try to return the new word:
def add_word(request, lecture_id):
l = get_object_or_404(Lecture, pk=lecture_id)
if request.method == "POST":
#see if there is a value with p
if request.POST.has_key('word') and request.POST['word'] != "":
success = {}
try:
oldWord = l.post_set.get(word=request.POST['word'])
except:
newWord = l.post_set.create(word=request.POST['word'], count = 1)
success = {'new': str(newWord.word), 'count': str(newWord.count)}
else:
oldWord.count += 1
oldWord.save()
success = {'old': str(oldWord.word), 'count': str(oldWord.count)}
return HttpResponse(json.dumps(success), mimetype="application/javascript")
return HttpResponse(reverse('post.views.lecture_display', args=(l.id,)))
Im getting a 500 error...
[13/Oct/2011 15:14:48] "POST /lecture/3/add HTTP/1.1" 500 66975
Without seeing the traceback, my guess is that what's failing is [one of]:
# A) This path is not resolving correctly (see named-URLs in Django's docs)
reverse('post.views.lecture_display', args=(l.id,))
# B) This word has unicode data, which can't simply be passed to ``str``
str(oldWord.word)
Open the URL directly in your browser, and you'll get the default Django traceback, 500 view.
I think you need to learn debugging rather that a particular fix.
If the problem persists, use ipdb
or pudb
package, insert the following line in the view and analyze what happens inside your code:
def myview(request, id): import ipdb; ipdb.set_trace()
Use Chrome Developer Tools or Firebug to see what the server outputs and what urls it opens. Also take a look at Django Debug Toolbar and Werkzeug. The debug toolbar can show you all the templates that were rendered and all the local variables. Werkzeug also gives you a debug shell in any place of the call stack right from the browser.