开发者

Calling a Django view using Dajax

开发者 https://www.devze.com 2023-03-26 15:30 出处:网络
I\'m new to both Django and Dajax, but I\'m having a problem with a very basic function using Dajax.Right now, I just want to post a form to Django and return a simple response showing that I reached

I'm new to both Django and Dajax, but I'm having a problem with a very basic function using Dajax. Right now, I just want to post a form to Django and return a simple response showing that I reached my endpoint.

However, I keep getting an error returned back to my form.

Here is my form:

{% load dajaxice_templatetags %}

<html>

    <head>
        {% dajaxice_js_import %}


        <script type="text/javascript">
            function my_js_callback(data){
                alert(data.message);
            }
        </script>
    </head>

    <body>  
        <form name="new_scan" action="" method="post">
            {% csrf_token %}                    

            <input type="text" id="scan_sku" name="scan_sku" maxlength="10" />
            <butt开发者_如何学Goon name="sku_lookup_btn" id="sku_lookup_btn" onclick="Dajaxice.inventory.views.test_ajax(my_js_callback);">Add to order</button>
        </form>     
    </body>

</html>

And here is the corresponding view:

@dajaxice_register
def test_ajax(request):
    return "Hello World"

I'm getting a javascript alert that says, "Something goes wrong"

Unfortunately, I don't know how to debug exactly "what went wrong".


The deal with dajax/dajaxice is: you don't define the dajaxice-callable 'views' in views.py; you create a separate ajax.py file in your app directory, and you define functions in there. These functions look and act a lot like Django view functions -- for example, if you import login_required from django.contrib.auth.decorators, it'll work on your dajax functions as it does when used on django views.

In fact, here is an ajax.py file that contains a single dajax function of mine, which searches a haystack index to populate a jQuery UI autocomplete field:

from dajax.core import Dajax
from dajaxice.core import dajaxice_functions
from haystack.query import SearchQuerySet
import ost2.portfolio.models as ax

def query(request=None, q=''):
    srcher = SearchQuerySet().models(ax.AXItem)
    dajax = Dajax()
    out = []
    valid = set()
    js_callback = "$$('#q')._callback"

    if q:
        results = srcher.autocomplete(everything=q.strip())
        bestmtch = results.best_match().uuid
        for result in results.order_by('order'):
            out.append(dict(
                value=result.title,
                label=result.object.title,
                notes=result.object.notes,
                thumburl=(result.object.keyimage and result.object.keyimage.thumbelina.url or ''),
                urlstring=result.object.urlstring,
                pk=result.object.pk,
                best=bool(bestmtch == result.uuid),
            ))

    dajax.add_data({'data': out, 'term': q}, js_callback)
    return dajax.json()

dajaxice_functions.register(query)

... in this case, I am not using dajaxice_functions.register() as a decorator -- I developed this function using an earlier version of dajaxice in which you had to register your functions explicitly.

One thing you should note is: the python Dajax class instance has a number of methods that map to some action on the client (function calls, DOM manipulation, etc); in order for those methods to properly work on the client side, you set the JavaScript function Dajax.process() as the callback (sans parens in your JS code).

Another thing is: I set the default value request=None in dajaxice python function, because that makes it much easier to debug. I don't know if you can construct stub requests in unit tests for dajaxice (I don't think you can) but since this particular function doesn't deal with request, it can be tested like this:

>>> from ost2.portfolio.ajax import query
>>> query(q='yo dogg')
[ ... ]

I would recommend that you try something like this after you get your ajax.py file set up, so you can test the python function independently of the JavaScript handlers and whatnot. Dajax and DajaxIce are great in many ways, but unit-test-ability is not one of them.

Good luck.


To see what is going on I propose to set up the logging facility for dajax:

Add a file handler in your settings.py under LOGGING

'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'filters': ['require_debug_false'],
        'class': 'django.utils.log.AdminEmailHandler'
    },  
    'logfile': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/path/to/your/log/django.log'
    },  

},

As second step add the dajaxice logger to the loggers

'loggers': {
   'dajaxice': {
        'handlers': ['logfile'],
        'level': 'DEBUG',
        'propagate': True,
    },
   'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },
}

If you're working on the console tail -f django.log outputs the logs live on the console ...

0

精彩评论

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