--
Ticket URL: <https://code.djangoproject.com/ticket/27882>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim Graham):
I didn't test it but it looks like it should work -- the template tag is
calling
[https://github.com/django/django/blob/fba4f831bc75369c975a95c9b7774e9e89f8a2f9/django/templatetags/cache.py#L47
cache.set(key, value, timeout)]. Can you check to be sure that the problem
isn't in your project?
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:1>
Comment (by MikiSoft):
Replying to [comment:1 Tim Graham]:
Yes, I'm sure that it's not a problem in my project because I've tested my
project on this this way:
Fist, I put in a view
`print(cache.get(make_template_fragment_key('test')))` and in
corresponding template `{% cache 0 test %}`, then I started the server and
I opened to that page. It printed me `None`, even after refreshing the
page.
So, I changed tag to be `{% cache 300 test %}` and I've opened that page
again. And suddenly it started printing content which I put under that
cache tag!
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:2>
* has_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1
* stage: Unreviewed => Accepted
Comment:
[https://docs.djangoproject.com/en/stable/topics/cache/#basic-usage The
documentation] says, "Passing in `None` for `timeout` will cache the value
forever. A timeout of 0 won’t cache the value." So `{% cache %}` needs to
allow `None`. It looks like this patch does the trick:
{{{ #!diff
diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py
index 3af6dc4..9e402a1 100644
--- a/django/templatetags/cache.py
+++ b/django/templatetags/cache.py
@@ -20,10 +20,11 @@ class CacheNode(Node):
expire_time = self.expire_time_var.resolve(context)
except VariableDoesNotExist:
raise TemplateSyntaxError('"cache" tag got an unknown
variable: %r' % self.expire_time_var.var)
- try:
- expire_time = int(expire_time)
- except (ValueError, TypeError):
- raise TemplateSyntaxError('"cache" tag got a non-integer
timeout value: %r' % expire_time)
+ if expire_time is not None:
+ try:
+ expire_time = int(expire_time)
+ except (ValueError, TypeError):
+ raise TemplateSyntaxError('"cache" tag got a non-integer
timeout value: %r' % expire_time)
if self.cache_name:
try:
cache_name = self.cache_name.resolve(context)
}}}
A test and perhaps a documentation update is also required.
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:3>
Old description:
> Apparently, I've discovered that `{% cache 0 name %}` doesn't work - it
> should cache the content wrapped with it forever i.e. until server
> restarts, but when I specify it like that then it's not caching it at
> all, and I don't see any other solution to this. So can someone make it
> to accept zero as a parameter for unlimited time, like it is in low-level
> API?
New description:
Apparently, I've discovered that `{% cache None name %}` doesn't work - it
should cache the content wrapped with it forever i.e. until server
restarts, but when I specify it like that then it's giving an error for
invalid input, and I don't see any other solution to this. So can someone
make it to accept `None` as a parameter for unlimited time, like it is in
low-level API?
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:4>
Comment (by MikiSoft):
Replying to [comment:3 Tim Graham]:
Yes you're right about that, it should be `None` instead of zero (I've
corrected the main post). Thanks for solving it!
I have one more question if you can answer me here (sorry for being a bit
off topic) - when can I expect a new Django release with such patch?
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:5>
* owner: nobody => Bo Marchman
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:6>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:7>
Comment (by Bo Marchman):
PR is at https://github.com/django/django/pull/8120, with a test and doc
changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:8>
* needs_better_patch: 0 => 1
Comment:
I left a few comments for improvement on the PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:10>
Comment (by Bo Marchman):
Requested changes made.
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"7a7b331cd5975477597dac4dec7ee0ddb67f59e0" 7a7b331c]:
{{{
#!CommitTicketReference repository=""
revision="7a7b331cd5975477597dac4dec7ee0ddb67f59e0"
Fixed #27882 -- Allowed {% cache %} to cache indefinitely.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27882#comment:12>