Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Django templates get cached in browser?

Let's say we have a request made in Django which renders my_template.html and the context of {foo: bar}

 return render(request, "my_template.html", {foo: bar})

Now based on the user activity foo and bar change. Let's say if a user has made his first request. Django will return the template and the corresponding {foo: bar} based on user activity. Let's say if the user had made a second request with a different activity.

Will Django send my_template.html from the server to the browser or will only the changed context be sent to the browser? Does the template get cached in the browser?

Case 1:

If the template gets cached in a browser if a code with some changes is deployed does it reflect in the client's browser? Does this work in the way where the md5 hash of both the templates is compared if they are not in sync then the template is downloaded from Django server?

Case2

If the template is rendered every time for every request then does that mean the bandwidth usage is not optimistic?

I read that in a node and angular the javascript bundles is cached in clients browser and only data(context {foo: bar}) calls are made to a server so the bandwidth usage is better and loading is faster is this true.

like image 619
electrodragon Avatar asked Jul 03 '18 16:07

electrodragon


People also ask

Does Django automatically cache?

Local Memory Cache Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.

Where are Django templates stored?

Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps. Alternatively, you can maintain a template folder for each app separately.

How does Django store cache data?

Database Cache If you would like to store cached data in the database, Django has a backend for this purpose. To save cached data in the database, you just need to create a table in the database by going to the settings.py file, setting BACKEND to django. core. cache.

How does Django handle cache?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.


1 Answers

Django is a server side framework. Templates are fully rendered in the server before they are sent to the client. As a result the browser does not know anything at all about the templates; only the fully rendered html.

But there's no point in comparing Django with Angular like this. You can't use Angular on its own without some kind of backend framework, which could well be Django - the two work well together.

like image 131
Daniel Roseman Avatar answered Oct 20 '22 09:10

Daniel Roseman