Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Django Asyc Views an ajax replacement?

I have some confusions and will like to clear them by asking some questions.

According to this Ajax definition:

Ajax is a set of web development techniques using many web technologies on the client side to create asynchronous web applications. With Ajax, web applications can send and retrieve data from a server asynchronously without interfering with the display and behavior of the existing page.

1. Isn't it the same as async views feature offered in Django 3.0?

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

2. What does this mean for Channels?

Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols, IoT protocols, and more. It's built on a Python specification called ASGI.

Can async django replace channels too?

Suggest things related to these topics with reasoning. e.g. Use AJAX with JSON (just an example)

I know that converting Django to async will take time so have this in mind when answering

like image 824
NONAME Avatar asked May 16 '20 15:05

NONAME


People also ask

Is AJAX and async the same?

Ajax is a very well known method for loading the content of a Web page without manually refreshing it. But the letter “A” in Ajax means asynchronous, meaning that you need to have a callback function that will return the results.

What is async view in Django?

Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.

Does Django use AJAX?

Another way of using Ajax in Django is to use the Django Ajax framework. The most commonly used is django-dajax which is a powerful tool to easily and super-quickly develop asynchronous presentation logic in web applications, using Python and almost no JavaScript source code.

Are AJAX calls async?

It's usually better to use asynchronous calls AJAX, which stands for asynchronous JavaScript and XML, is a technique that allows web pages to be updated asynchronously, which means that the browser doesn't need to reload the entire page when only a small bit of data on the page has changed.


Video Answer


2 Answers

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:

  • https://oms.api/covid/stats/country/{country-id}

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:

  • (china) https://oms.api/covid/stats/country/ch
  • (usa) https://oms.api/covid/stats/country/eu
  • (italia) https://oms.api/covid/stats/country/it

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 :D

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:

  • https://docs.djangoproject.com/en/3.1/topics/async/
  • https://www.encode.io/articles/hello-asgi

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.
like image 174
JRichardsz Avatar answered Oct 24 '22 19:10

JRichardsz


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.

like image 39
mjschuetze102 Avatar answered Oct 24 '22 18:10

mjschuetze102