Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching behavior - possibly incorrect #43

Open
aschriner opened this issue Sep 24, 2013 · 3 comments
Open

Caching behavior - possibly incorrect #43

aschriner opened this issue Sep 24, 2013 · 3 comments

Comments

@aschriner
Copy link

I am using a foreignkey to dbtemplates Template model to determine how to display another model. ie.

class MyModel(models.Model):
    template = models.ForeignKey(Template)
    ...

When I switch the template field on MyModel to another template via the admin, MyModel is not getting the correct template for display. I get "TemplateDoesNotExist" sometimes. Other times it continues to display using the previous template. It never updates to the new template though. I have to use the admin action "repopulate cache with selected items" to get it to find the template again.

I am using a class based view and defining "get_template_names()" to get the Template model from the foreignkey.

Shouldn't the template be updating when the foreignkey is changed? And shouldn't it find the correct one either in the cache or via the database?

(Django 1.5)

@jezdez
Copy link
Member

jezdez commented Sep 25, 2013

No, it should not update the cache if your ForeignKey is changed. To quote the docs: "dbtemplates uses Django’s default caching infrastructure for caching, and operates automatically when creating, updating or deleting templates in the database". That applies to changing the templates, but not to setting relations to a template.

As to what you're trying to accomplish, I'm out of ideas. If you're simply changing the relation to a template, and use the template's name field in your get_template_names view method, I don't see how the cache needs to be invalidated at all. Each template has separate names after all, so the template loader will now what to do when it tries to load the template form the database.

@aschriner
Copy link
Author

Good point about caching and ForeignKey relations. Maybe the issue is not related to caching? As you describe, I am returning the template's '' field in '<get_template_names>'. (I also have a default template as a fallback because the template field is not required).

So here's the sequence I just went though.

  1. No template set on model (ie foreignkey is empty) -> loads default template in browser, no problem
  2. Using admin, update template field on model, set to existing Template object in database ("foo/sometemplate.html"), save model. Still loads default template in browser (not sure why this is - I think this problem is in my code though).
  3. Restart server process
  4. Try to load page, error TemplateDoesNotExist (correctly returns the name of the Template object I chose "foo/sometemplate.html", however).

Debug Template-loader postmortem says:
Django tried loading these templates, in this order:

* Using loader dbtemplates.loader.Loader:
* Using loader django.template.loaders.filesystem.Loader:
* Using loader django.template.loaders.app_directories.Loader: 
    * places
    * places
    * etc
  1. Go to admin page, run "repopulate cache with selected templates"
  2. page loads with correct template from dbtemplates

@aschriner
Copy link
Author

Further info:

Forgetting about the part with changing foreignkeys on my models...

If I leave the foreign key alone, and have no cache settings in my settings.py, I will get TemplateDoesNotExist when I try to load a page with that template. Then I go to the admin, "Repopulate cache with selected template", and the page loads ok. Wait a few minutes, and then try to load page again. I get TemplateDoesNotExist again.

So without cache settings created in settings.py, I'm not sure where the "Repopulate cache with selected template" is loading the template to. But it seems that the Loader is not correctly falling back to getting the template from the database if it fails to find it in the cache.

Hope that helps pinpoint the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants