Reputation: 509
Under django v. 1.4.
The problem:
Since my template is rendered by this view which will be totally stored in cache:
@cache_page(60*60*24)
def index(request):
foo_form = FooForm()
context = RequestContext(request, {
'foo_form': foo_form
})
# An entire page is rendered
return render_to_response('index.html', context_instance=context)
In my template I have an if statement which checks whether user is authenticate:
...
<li>
{% if user.is_authenticated %}
<a href="{% url 'home' %}" class="login">Enter</a>
{% else %}
<a href="" class="login" data-target="#login_modal" data-tggle="modal">Enter</a>
{% endif %}
</li>
...
There is a modal that's activated by a button "Enter" which should be displayed when there's no user logged, otherwise the user is redirected to the system when the "Enter" button is clicked.
The question: Is there a way to ignore only that piece of code from my template to be not cached? If so, how?
Upvotes: 2
Views: 558
Reputation: 6009
Use ajax to display the user authentication template, so caching wont affect your template.
<li>
`{% if user.is_authenticated %}`
` <a href="{% url 'home' %}" class="login">Enter</a>`
`{% else %}`
`<a href="" class="login" data-target="#login_modal" data-tggle="modal">Enter</a>`
{% endif %}
</li>
Upvotes: 0
Reputation: 7460
You should use Template fragment caching: https://docs.djangoproject.com/en/1.6/topics/cache/#template-fragment-caching
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
Upvotes: 1