Django Page Caching (Trouble with db)?
I'm having a bit of an odd issue in Django 1.2.4.
I have a page that lets the user declare front end settings. These settings are represented in the following models:
class Setting(models.Model):
"""
Maintains a key-value pair for some front-end editable setting.
"""
class Meta:
abstract = True
name = models.CharField(max_length=120, primary_key=True)
description = models.CharField(max_length=300, blank=True)
class IntegerSetting(Setting):
value = models.IntegerField()
class ListSetting(Setting):
value = PickledObjectField(default=lambda: list(), editable=True)
These settings are reflected in an admin change form. I have overridden the template, and make the setting available to the page as follows:
{% extends 'admin/change_form.html' %}
{% block extrahead %}
<script type="application/javascript">
var global= {};
global.min_slider = {{min_slider}};
global.max_slider = {{max_slider}};
</script>
{{ block.super }}
{% endblock %}
These values are provided from an admin class:
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
min_max = {'min_slider': get_setting("Min Slider Value"), 'max_slider': get_setting("Max Slider Value")}
def change_view(self, request, object_id, extra_context=None):
extra_context = extra_context or {}
extra_context.update(self.min_max)
return super(FooAdmin, self).change_view(request, object_id, extra_context)
After I change a setting on the setti开发者_如何学运维ngs form, I don't see it updated on any page load of the admin change form. However, after restarting the dev server, I see the data just fine.
What is going on here? Am I running into a caching issue? If it's caching in the admin interface, how can I disable it for just that one page?
The problem is that you are defining these at the class level in the admin. They therefore get evaluated when the class definition is executed, ie at process startup.
Move the dictionary definition into the change_view
method itself.
Do you save the setting in the session? Did you update the session after changing the setting?
(That was my problem just a few days ago.)
精彩评论