-1

Basically, I am a Wordpress developer. When i make an ajax request in Wordpress, hook up wp-admin ajax with the request.

I am pretty new in Django. Does Django has any helper library for back-end implementation through which it hooks up ajax request. Wordpress uses following code to generate url for $.ajax-

admin_url( 'admin-ajax.php' )

If i explain bit further, then it may be -

In Wordpress, if i use the URL in AJAX then admin ajax url is loaded which hooks up an action to catch the request. I am wondering is there something already implemented as a helper library for handling admin ajax call in Django

This is just my curiosity that Django has something like that?

Thanks!

user3384985
  • 2,975
  • 6
  • 26
  • 42

1 Answers1

2

Websites can process AJAX requests, including ones that are built with Python and Django.

Since you are a WordPress-centric developer, making the leap from WordPress/PHP over to Python will be challenging. However, let's see if we can get you started.

jQuery or JavaScript's AJAX handling doesn't care what language or framework/app/library is running on the server side. It packages up the dataset, configures what it wants back, and then sends it to an endpoint on the server.

What you need to do on the server side then is handling:

  • Routing
  • Validation
  • Request processing
  • Returning back a serialized packet

Disclaimer: It appears you are speaking about non-RESTful WordPress implementation. Therefore, I'll make the comparisons using that approach.

WordPress handles the routing for you. You point your request to the endpoint of the admin URL. Then you register your processing callback to the event name wp_ajax_{your-action}. The action is defined in the AJAX packet you sent to the server. Next, you do your validation with a nonce check. Then you run your code to process the request, build any HTML, pass it through a JSON serializer, and send it back to the browser.

Let's relate WordPress over to Django for you:

Routing - Endpoint

In Django, you need to declare the endpoint so that it knows who to do the routing. Then you can use that endpoint in your AJAX request. It's an extra step that you don't do in WordPress.

Validation

WordPress uses nonce. Django you will use csrf_token.

Processing

You write you code in Python that handles the processing for the request. WordPress you write in PHP.

Send it Back

Django provides you with JSON, XML, and YAML serializers. You'll likely use JSON. This will package it up for the return. Then you can send it back.

Some Tutorials to Help

You won't find any that will convert WordPress' implementation over to Python or Django. Instead, you'll want to learn about AJAX first to ensure you understand how it works and what is required. Then you can set off to architect a test in Django.

Here are some tutorials to help you:

Community
  • 1
  • 1
hellofromTonya
  • 1,301
  • 8
  • 8
  • Hi, thanks for your answer. You really got me. What was my confusion that distribution of URL in ajax request. Ajax calling procedure doesn't vary with platforms. When we make ajax request there is the question to where we are really sending the request. In Wordpress, they handle that through wp-admin ajax. So i was confused how can i do that. From your given first link, it is well described about the issue. So, when we visit a website page the request URL would be same as when we make the request through ajax. – user3384985 Nov 27 '16 at 17:58