简体   繁体   English

如何在Django视图中禁用缓存?

[英]How to disable cache in django view?

Whenever I modify the django view or static files, it may still not update when I reload the page. 每当我修改django视图或静态文件时,当我重新加载页面时,它仍然可能不会更新。

For example, 例如,

def index(request):
    print 'first'
    return httpResponse('ok')

The django server log returns 'first' django服务器日志返回“ first”

But After modification 但是经过修改

def index(request):
    print 'second'
    return httpResponse('ok')

The django may still returns 'first', not 'second'. Django可能仍会返回“第一”,而不是“第二”。

I have found some way to disable cache 我找到了禁用缓存的方法

for example, 例如,

from django.views.decorators.cache import never_cache

@never_cache
def index(request):
    print 'second'
    return httpResponse('ok')

But it will not work. 但这是行不通的。

How can I avoid this problem when in development mode? 在开发模式下如何避免此问题?

Thank you! 谢谢!

This is nothing to do with caching. 这与缓存无关。 Your devserver has not detected the change for some reason, so has not restarted; 您的devserver由于某种原因未检测到更改,因此尚未重启; just do ctrl-c and re-run it manually. 只需执行ctrl-c并手动重新运行即可。

In Chrome , go to your browser settings, and you can Disable cache . Chrome中 ,转到您的浏览器设置,然后可以Disable cache It is the first options under settings > general 这是设置>常规下的第一个选项

Hm, Django dev server detects changes in .py files that it loads when starting. 嗯,Django开发服务器在启动时会检测到它加载的.py文件中的更改。 Could you try to add 你可以尝试添加

from your_app import index

for instance in your urls.py, just to be sure module is loaded. 例如在您的urls.py中,只是要确保模块已加载。

Then modify index code. 然后修改索引代码。

Your devserver should restart. 您的devserver应该重新启动。

If it restarts, your modifications will be taken into account and your print should be ok 如果重新启动,您的修改将被考虑在内,并且您的打印应该可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM