Does Django create cache templates automatically?

I am new to Django and am trying to implement a voting system between two images. However, it looks like the page is being cached or something else, because when I refresh it, some values ​​are wrong. I do not have cache settings in my settings.

Here is a view:

def rate(request, type): photos = Photo.objects.order_by('?')[:2] c = Context({"photos": photos, "type": type}) return render_to_response("base_rate.html", c) 

and pattern:

 {% extends "base.html" %} {% block body %} <div class="photo"> <img src="{{photos.0.photo.url}}" alt="Photo" /> <a href="/rate/vote/{{photos.0.id}}/{{photos.1.id}}" class="vote">Vote</a> <a href="/rate/flag/{{photos.0.id}}" class="flag">Flag</a> </div> <div class="photo"> <img src="{{photos.1.photo.url}}" alt="Photo" /> <a href="/rate/vote/{{photos.1.id}}/{{photos.0.id}}" class="vote">Vote</a> <a href="/rate/flag/{{photos.1.id}}" class="flag">Flag</a> </div> {% endblock %} 

Some pages will contain incorrect information for objects. Here is an example of the source I get:

 <div class="photo"> <img src="/img/rate/16photo1.jpg" alt="Photo" /> <a href="/rate/vote/16/17" class="vote">Vote</a> <a href="/rate/flag/16" class="flag">Flag</a> </div> <div class="photo"> <img src="/img/rate/17photo2.jpg" alt="Photo" /> <a href="/rate/vote/16/16" class="vote">Vote</a> <a href="/rate/flag/16" class="flag">Flag</a> </div> 

The second Vote href should be "/ rate / vote / 17/16", and the href flag should be "/ rate / flag / 17", but something goes wrong, and I get inconsistent data.

Any ideas?

+4
source share
3 answers

Having looked at this in some of my code, I have this in my template:

 {{ mytable.0.pk }} {{ mytable.1.pk }} {{ mytable.0.pk }} {{ mytable.3.pk }} 

And I get this output:

 91596 54774 156800 23593 

Odd until you find django to execute database queries is very lazy. This is what appears in my mysql log to load a single page:

 SELECT `mytable`.`id` FROM `mytable` ORDER BY RAND() LIMIT 1 SELECT `mytable`.`id` FROM `mytable` ORDER BY RAND() LIMIT 1 OFFSET 1 SELECT `mytable`.`id` FROM `mytable` ORDER BY RAND() LIMIT 1 SELECT `mytable`.`id` FROM `mytable` ORDER BY RAND() LIMIT 1 OFFSET 3 

Each time you use dotted notation, it executes an entire new query. I would suggest changing your code like this:

 def rate(request, type): photos = list(Photo.objects.order_by('?')[:2]) c = Context({"photos": photos, "type": type}) return render_to_response("base_rate.html", c) 

Since list() forces you to evaluate, it will execute the request right then and there. In addition, the data for both of these elements is already cached, so there is no reason to hit the database again. You should be good to go.

+8
source

order_by('?') means that the list is ordered in random order, so {{ photos.0 }} will be different every time the page loads.

also:

 <a href="/rate/vote/{{photos.1.id}}/{{photos.0.id}}" class="vote">Vote</a> ^^^ ^^^ 

This seems to be wrong.

+1
source

Django does not cache such things by default. Make sure your browser is / isp / etc. Don't cache it.

It looks like your database query is not returning what you expect. Check this directly through your debugger or debug your print expression if you need to.

This says that you really need to think long and hard about your application design. Using a GET request to make changes to the state of your application is an incredibly bad idea. Especially how you do it. You need to change these links to POST requests in one form. Otherwise, you will find that random web spiders destroy your application.

+1
source

All Articles