django static files versioning

后端 未结 10 1060
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 21:26

I\'m working on some universal solution for problem with static files and updates in it.

Example: let\'s say there was site with /static/styles.css file - a

10条回答
  •  野的像风
    2021-01-30 22:04

    I use a global base context in all my views, where I set the static version to be the millisecond time (that way, it will be a new version every time I restart my application):

    # global base context
    base_context = {
        "title": settings.SITE_TITLE,
        "static_version": int(round(time.time() * 1000)),
    }
    
    # function to merge context with base context
    def context(items: Dict) -> Dict:
        return {**base_context, **items}
    
    # view
    def view(request):
        cxt = context({<...>})
        return render(request, "page.html", cxt)
    

    my page.html extends my base.html template, where I use it like this:

    
    

    fairly simple and does the job

提交回复
热议问题