开发者

Why is my dajax-powered loading another page?

开发者 https://www.devze.com 2023-04-12 07:09 出处:网络
I\'m working through the random machine example, but I\'m having trouble. I\'ve installed Dajax and Da开发者_JS百科jaxice according to these two pages: Dajax and Dajaxice. My ajax.py file looks like t

I'm working through the random machine example, but I'm having trouble. I've installed Dajax and Da开发者_JS百科jaxice according to these two pages: Dajax and Dajaxice. My ajax.py file looks like this:

import random
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def randomize(request):
    dajax = Dajax()
    dajax.assign('#result','value',random.randint(1, 10))
    return dajax.json()

Here's my views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext

def dajax(request):
     return render_to_response( 'dajax.html', context_instance=RequestContext(request) )

Here's my urls.py:

from django.conf.urls.defaults import patterns, include, url
from dajaxice.core import dajaxice_autodiscover
from dajaxice import urls
from django.conf import settings
from Dajax.myapp.views import dajax
dajaxice_autodiscover()

urlpatterns = patterns('',
    (r'^%s/' % (settings.DAJAXICE_MEDIA_PREFIX), include('dajaxice.urls')),
    (r'^dajax/$', dajax),
)

My html form tag looks like this:

<form>
        <input type="text" name="result" value="" id="result">
        <input type="button" name="rand" value="Let's Rand!" id="rand" onclick="Dajaxice.myapp.randomize(Dajax.process); return false;">
</form>

Here's what my head tag looks like:

<head>
    <title>My base template</title>
    {% dajaxice_js_import %}
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript" charset="UTF-8"></script>
    <script src="{{ STATIC_URL }}js/jquery.dajax.core.js" type="text/javascript" charset="UTF-8"></script>
</head>

I added the following to my init.py file in my project dir:

from myapp.ajax import randomize

Also of note, my Django app's name is myapp.

On my page, when I click on Let's Rand!, the page goes to http://localhost:8000/dajax/?result=2, and no value shows up in the text input box. I checked the console in Chromium and there aren't any errors.

Did I incorrectly install either Dajax or Dajaxice, or is there something wrong with my code above?

My guess is that because I'm using jQuery instead of Prototype, there's something different I need to do.

Thanks for your help!


You need a return false; in your onClick handler after the call to Dajax.


put this in your views.py :

from dajaxice.core import dajaxice_autodiscover
dajaxice_autodiscover()


You need to make sure that the app you are trying to import the ajak.py file from is in your INSTALLED_APPS in settings.py.

See the source for autodiscover:

http://docs.dajaxproject.com/dajaxice/_modules/dajaxice/core/Dajaxice.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号