Django template won't update

家住魔仙堡 提交于 2020-04-10 09:13:11

问题


I have a django base template and I added a few JS libraries, but every time I refresh the page no changes are shown.

  • I have already tried deleting my browser's cache and history and all that.
  • I'm not using django cache anywhere in my code, is it posible that a configuration in settings.py may be causing this behavior? (I've already checked that file and there's nothing related to cache...)
  • I've already restarted django development server and tried, nothing to do there...

Is there anything else I can do to make django realize that my base template has changed and I don't want the old one? Where does django store does cached files? I don't know what else to do...

[EDIT]

This is embarassing... It turns out that I copied my project from one folder to another, but didn't update the template_dirs directive in the settings.py, hence, django was using the old templates.

I solved this removing all hardcoded paths from settings.py and adding getcwd() function to create the paths, now I can move my project folder anywhere and it still get it to work fine.

Sorry for all the trouble


回答1:


From my experience using Django, the problem is caused by having a duplicate file therefore when you edit one file, django is fetching the other. Removing duplicates should solve your problem.




回答2:


By default, the Django template loader caches templates when DEBUG = False. After changing a template, you need to restart the server (e.g. Apache or gunicorn) to see the changes. This behaviour is not affected by the CACHES setting. Try changing DEBUG = True




回答3:


Is everything else in the base template showing up? Check the source of the page you're rendering and see if everything in the base template is showing up except the new modifications (js libraries).

If it's not there, add {% extends "base.html" %} (or whatever the name of the base template is) as suggested by Randall Ma to the top of the template file being rendered. Your base template file (e.g. base.html) is not automatically added to each extended template - you have to explicitly declare it above.

If that doesn't solve it, you'll have to post your source files like ArgsKwargs suggested.




回答4:


I made a small change to django.views.static.serve that solved most of my issues with getting stale resources in the browser. It might be worth a try:

http://lee-phillips.org/djangoStatic/




回答5:


Try using runserver instead of apache/nginx




回答6:


By default, Django loads templates from the disk. http://www.jongales.com/blog/2012/02/16/make-django-keep-templates-in-memory/ So in theory, any changes to a template should be immediate. As @daneilrvt said, his issue was not with Django, but rather a change he made that he did not update. I had the same issue this afternoon. So when it comes to changes not being updated, be sure to double check that you are updating the correct template or piece of code in the template.



来源:https://stackoverflow.com/questions/11704068/django-template-wont-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!