Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template rendering on the client side

I have a template for the search results page, which in general looks like this:

{% for result in results %}
  // single result template
{% endfor %}

As I understand, when user requests for this page from browser, the template renders on the server side, and then html transfers to the user. It seems to me, that one can decrease the traffic by sending just JSON with values and obtaining the final html on the client side. The profit is due to not sending the same html-core for each template.

Does that make sense? Is there any common solution for such optimization? Thanks a lot.

like image 892
typedef Avatar asked Mar 22 '23 17:03

typedef


1 Answers

Yes, there are, although not as part of stock Django. Frameworks such as Ember.js and AngularJs do this in Javacript. These both are a complete Model View Controller framework which runs client side. Ultimately the Serverside can simply be reduced to a REST interface transferring json to the clientside.

This is an extreme example, and I believe that Ember.js can easily be integrated with just small sections of your site which may be what you are looking for to use with django.

If django is using caching middleware, then it should not be too inefficient for most sites, as the cache will only be updated when the data changes, although generating dynamic search results is a good example of when this is not true.

The django templating system is very efficient. As with most web frameworks, in the majority of cases, when executing a search on the server the majority of the time spent servicing a request will be used in querying the database, especially if plain text searching is used. Therefore optimising the final step of servicing the request in the form of template rendering, probably wouldn't be a worthwhile optimisation for most cases.

Having said that many websites do perform rendering clientside, and it is currently an active area of framework development.

like image 77
Henry Florence Avatar answered Apr 02 '23 01:04

Henry Florence