[Solved]-Is Django Asyc Views an ajax replacement?

11👍

I’m not a python developer but I implemented a couple of web server from the scratch and I think I can help you.

Server Rendering vs Client side Rendering

In web development there are two approaches to deliver content to end users, called Server rendering and client side rendering

Server side rendering (SSR) — the traditional rendering method, basically all of your page’s resources are housed on the server. Then, when the page is requested (commonly from web browsers), the Html, JS and CSS are downloaded. Also frameworks can dynamically can create the html based on backend logic and finally download it. At this point, a lot of frameworks offer wonders for creating apps in no time with "amazing" functionalities.

Technologies : java, c#, python, nodejs, etc

Client side rendering (CSR) — Which is sometimes called "Frontend rendering" is a more recent kind of rendering method, this relies on JS executed on the client side (browser) via a JavaScript framework. So, when page is requested, a minimal , little or empty index.html, css and js were downloaded. Here javascript is responsible to send or receive data and update a minimal section of the page without an entire page refresh.. Finally when user click or trigger some event, javascript will send or receive the data commonly to an api rest (json) using an async call (ajax).

Technologies : react, angular, vue, aurelia, jquery, pure javascript, etc

Django is Server Rendering Framework

As you can see this posts: Simplest CRUD example and Hello World app, you need python (server language) to develop in Django. Django internally creates your html pages and render them to your user.

React(angular, vue, etc) is a Client side Rendering framework

Imagine an api provided by OMS. This api offer us and endpoint to get covid stats by country:

Imagine you are from generation z, and you don’t know about java, python, c# and other ancients languages. You need to develop a simple dashboard showing covid stats by first countries who contracted the virus.

Your web will have a visual cool effect : Load home page with empty boxes and one by one, you will show the stats starting for the top ten countries.

To make this effect you will use React to render a home page with empty boxes and you will trigger several requests to the api:

Your home page is still working, user is navigating, scrolling and after some seconds, boxes are filled with stats.

So, we can say that your web performed ASYNCHRONOUS calls allowing for parts of web pages to be loaded dynamically…wait wait This is AJAX 😀

Async Views : Python async code with ASGI

One of new challenges for Python web frameworks is adapt to the potential benefits of an asynchronous model.

Django has support for writing asynchronous ("async") views, along with an entirely async-enabled request stack if you are running under ASGI.

The ASGI specification is an iterative but fundamental redesign, that provides an async server/application interface, with support for HTTP, HTTP/2, and WebSockets.

As you can see in the following links, Async Views are not html pages with ajax, since use ASGI, we can say that is an attempt of Django to develop in a async way but IN THE SERVER with python:

Conclusion

Async Views are not html pages with ajax, are just a python code but in async way running in the server.

Your questions

  1. Async Views vs AJAX?
  • Async Views are code in a python (server) and ajax in a browser. Surely Async View and Ajax are not related.
  1. Isn’t this is what asynchronous views in Django will do too?
  • Yes, Async Views is a kind of asynchronous code but in the server, not in the client side like react, angular, etc
  1. If Async Django will not completely replace AJAX, Is it worth learning?
  • It depends of your goals. Currently jobs for web developers (check remote jobs sections of this page –>) are requesting async skills but in client side like react, angular, vue, etc. Java, python and another powerful languages are using its power for backend requirements.
  1. What does this mean for Channels?
  • As your paragraph, channel could be for apis and webs but with server strategy (python).
  1. Can async django replace channels too?
  • I think no. Maybe channels are using or will use async techniques like Async Views.

3👍

According to info I have, (don’t know what AJAX is but many Django tutorials mention it so it was on my learning wishlist)

AJAX stands for Asynchronous Javascript and XML. It boils down to performing asynchronous calls allowing for parts of web pages to be loaded dynamically. This means that only part of the page is refreshed when data is changed rather than the whole page. It allows you to have your page fetch data from a server or API and allow the user to still view the page simultaneously. Once the information is retrieved from the API, the view can be updated to match the new data.

Isn’t this is what asynchronous views in Django will do too?

Yes, it looks like Django will allow for you to perform these sorts of techniques within the views themselves. There may be some use cases where AJAX can be used and Django’s async feature can not, but without looking into it, I can not give a definitive answer.

1A. If Async Django will not completely replace AJAX, Is it worth learning?

Is AJAX still worth learning. I would say yes. I’m of the mindset of jack of all trades, master of none. If you plan on spending the rest of your life developing with Django, maybe it’s not worth it. AJAX is very common to find throughout other technologies however, so if you plan on venturing outside of the Django stack, it would be worth learning. You would also have the added benefit of knowing some of the underlying principles of how asynchronous communication works if you delve deeper into AJAX.

  1. What does this mean for Channels?

Again, only speculation, but I’m assuming Channels will provide additional functionality that async Django will not have right out of the gate (in your excerpt you mention: handle WebSockets, chat protocols, IoT protocols, and more. Django being async does not inherently provide these functionalities). Over time, Django might adopt some of these features, but I would assume Channels will still have its niche.

Leave a comment