Login

Tag "ajax"

Snippet List

Load dynamically loaded form javascript assets using dajax

I am used to load forms directly into modals using dajax but I found out I had to load the scripts using an ajax call from the browser. You can see here an example of a dynamically loaded form and the function used to load the scripts.

  • ajax
  • javascript
  • forms
  • script
  • dynamically-loaded-form
  • form-assets
  • dajax
Read More

JSON decorator for views handling ajax requests

Sample usage for using decorator `@json_response(ajax_required=True, login_required=True)` `def subscribe(request):` ` return {"status":"success"}` Converts a function returning dict into json response. Does is_ajax check and user authenticated check if set in flags. When function returns HttpResponse does nothing.

  • ajax
  • json
  • decorator
  • jsonp
Read More

django ajax_view decorator

All credit goes to Peter Coles. [http://mrcoles.com/blog/decorator-django-ajax-views/#c281](http://mrcoles.com/blog/decorator-django-ajax-views/#c281) I slightly modified the original code to work without passing parameters to decorator. I will delete this post if anyone does not want this snippet to be posted.

  • ajax
  • ajax view decorator
Read More

Workaround Firefox bug 553888

Firefox transparently follows redirects when AJAX calls return 3xx code. And it drops additional headers, X-Requested-With among them. Server treats redirected HTTP requested as non-AJAX. JS libraries has nothing to do here. At 16.03.11 bug https://bugzilla.mozilla.org/show_bug.cgi?id=553888 has status "NEW" being reported at 21.03.10. Workaround is following: - in process_response(): if request.is_ajax() and response has status_code 3xx then put response["Location"] to session, otherwise unset session stored value (if it is there). - in process_request(): if not request.is_ajax() and request.path equals to stored session value then monkeypatch request.is_ajax() return True (before any views come into play). This results in smooth transparent redirects in Firefox, all treated as AJAX calls.

  • ajax
  • redirect
  • headers
Read More

AjaxForm Base Classes

These base form classes add a method to return error messages as HTML encoded as JSON from Django, optionally passing in an argument to strip tags out. The method can be called in your view after checking that your form is valid. There is a ModelForm and Form class to use depending on your needs. The sample jQuery function will take the errors returned as json, loop over the errors and insert the error after each field. If you're using a form prefix, you'll need to add a hidden field to hold the value for the prefix. The `__all__` error(s), which are not bound to a field are appended to the end of the form, which you could easily reposition. Happy coding!

  • ajax
  • json
  • form
Read More

Class-Based AJAX fallback view

If you're used to auto_render like I am (and think it's an awesome way to DRY code) and you want to support users with and without javascript enabled, this class is perfect. You will need to provide the format requested, either through adding "(\.format)?" at the end of the url or through some middleware, but once you've done so this class will take care of rendering through the given template for normal users, JSON or JSONP for AJAX users. From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)

  • ajax
  • view
  • json
  • class
  • fallback
  • class-based
Read More

Improve ajax progress bar by jquery extensions

This snippet in the one to improve the method described in [http://djangosnippets.org/snippets/679/](http://djangosnippets.org/snippets/679/). It uses some jquery extensions to make the task more easier: **jquery.timers.js**, **jquery.progressbar.js**, **jquery.form.js**.

  • ajax
  • javascript
  • jquery
  • upload
  • file
  • progress
  • jquery extension
Read More

Ajax ordering models on the change list page of the admin using drag and drop with jQuery UI

Makes models orderable on the change list page of the admin using drag and drop with jQuery UI (via sortable()). So you can order your objects in more easy way. Inspired by snippets [#1053](http://djangosnippets.org/snippets/1053/) and [#998](http://djangosnippets.org/snippets/998/) First, ordering field to your model (default called 'order). You can specify other name for this field, but you should add 'order_field' attr to model (i.e order_field = 'your_new_order_field_name') Also, snippet adds 'order_link' field to admin's changelist and hides it by javascript.

  • ajax
  • admin
  • jquery
  • ordering
Read More

Dojo Helper

This module contains functions and classes that help integrate the Dojo Toolkit javascript library with Django. Supports defining theme, stylesheets, required modules, and addOnLoad functions from Django. Django form fields can be instrumented to be instantiated as Dijit form objects programmatically. Full instructions on [limscoder.com](http://www.limscoder.com/2010/04/django-dojo.html).

  • ajax
  • javascript
  • dojo
Read More

Ajax auto-filtered Foreign Key Field

Say you have a ModelChoiceField and you want the choices to depend on the value of another field in the form... that's what these bits are for. They need to be used in conjunction with the Ajax views from: [code.google.com/p/django-ajax-filtered-fields/](http://code.google.com/p/django-ajax-filtered-fields/) See my blog for full details: [anentropic.wordpress.com](http://anentropic.wordpress.com) ...um, this is work in progress and the explanatory blog post is not up yet...

  • ajax
  • widget
Read More

51 snippets posted so far.