How do I include image files in Django templates?

前端 未结 11 1687
时光说笑
时光说笑 2020-11-27 10:35

I\'m new to Django and I\'m trying to learn it through a simple project I\'m developing called \'dubliners\' and an app called \'book\'. The directory structure is like this

11条回答
  •  被撕碎了的回忆
    2020-11-27 10:51

    In development
    In your app folder create folder name 'static' and save your picture in that folder.
    To use picture use:

    
        
           {% load staticfiles %} 
        
        
            
        
    
    

    In production:
    Everything same like in development, just add couple more parameters for Django:

    1. add in settings.py
      STATIC_ROOT = os.path.join(BASE_DIR, "static/")(this will prepare folder where static files from all apps will be stored)

    2. be sure your app is in INSTALLED_APPS = ['myapp',]

    3. in terminall run command python manage.py collectstatic (this will make copy of static files from all apps included in INSTALLED_APPS to global static folder - STATIC_ROOT folder )

      Thats all what Django need, after this you need to make some web server side setup to make premissions for use static folder. E.g. in apache2 in configuration file httpd.conf (for windows) or sites-enabled/000-default.conf. (under site virtual host part for linux) add:

      Alias \static "path_to_your_project\static"

      Require all granted

      And that's all

提交回复
热议问题