1. 程式人生 > >django 設置靜態文件,static

django 設置靜態文件,static

wan 進行 index styles static poll doc tin tex

django 設置靜態文件,static

一、搜集靜態文件

1.1 命令行查看 collectstatic

guoguos-MacBook-Pro:mysite guoguo$ python manage.py -h

[staticfiles]
    collectstatic
    findstatic
    runserver

1.2 執行 python manage.py collectstatic 收集信息
guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified 
in your settingsguoguo. This will overwrite existing files! Are you sure you want to do this? Type yes to continue, or no to cancel: yes Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/__init__.py
", line 364, in execute_from_command_line utility.execute() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/base.py
", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle collected = self.collect() File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect handler(path, prefixed_path, storage) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 354, in copy_file if not self.delete_file(path, prefixed_path, source_storage): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 260, in delete_file if self.storage.exists(prefixed_path): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/files/storage.py", line 392, in exists return os.path.exists(self.path(name)) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/storage.py", line 50, in path raise ImproperlyConfigured("You‘re using the staticfiles app " django.core.exceptions.ImproperlyConfigured: Youre using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. guoguos-MacBook-Pro:mysite guoguo$ python manage.py -h 1.3 解決報錯 發現報錯,在setting.py 中沒有設置 STATIC_ROOT,ok,下面進行設置 在setting.py 底部設置配置 如下 STATIC_URL = /static/ STATIC_ROOT = os.path.join(BASE_DIR, static) 1.4 再次執行,查看結果 guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic 過程如下,這部分是把django 默認自帶的 static 文件 搜集 放到該項目中,最後顯示搜集62項 Copying /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/admin/static/admin/css/base.css Copying /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/xregexp/xregexp.min.js Copying /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/static/polls/style.css 62 static files copied to /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static. 1.5 在 polls 項目發現有個多了個static 目錄,裏面顯示上面搜集到的信息 └── static ├── admin │ ├── css │ │ ├── base.css │ │ ├── changelists.css │ │ ├── dashboard.css │ │ ├── fonts.css │ │ ├── forms.css │ │ ├── login.css │ │ ├── rtl.css │ │ └── widgets.css │ ├── fonts │ │ ├── LICENSE.txt │ │ ├── README.txt │ │ ├── Roboto-Bold-webfont.woff │ │ ├── Roboto-Light-webfont.woff │ │ └── Roboto-Regular-webfont.woff │ ├── img │ ├── jquery │ │ ├── LICENSE-JQUERY.txt │ │ ├── jquery.js │ │ └── jquery.min.js │ └── xregexp │ ├── LICENSE-XREGEXP.txt │ ├── xregexp.js │ └── xregexp.min.js 二、自定義static文件 2.1 創建自定義static文件 │ ├── static │ │ └── polls │ │ ├── images │ │ │ └── osd.png │ │ └── style.css 2.2 編輯css 內容 設置 字體顏色和 背景圖片 guoguos-MacBook-Pro:polls guoguo$ cat style.css li a { color: green; } body { background: white url("images/osd.png") no-repeat right bottom; } 2.3 編輯index.html文件內容 增加如下: {% load static %} <link rel="stylesheet" type="text/css" href="{% static ‘polls/style.css‘ %}" /> 內容 guoguos-MacBook-Pro:polls guoguo$ pwd /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/templates/polls guoguos-MacBook-Pro:polls guoguo$ cat index.html {% load static %} <link rel="stylesheet" type="text/css" href="{% static ‘polls/style.css‘ %}" /> {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li> <a href="{% url ‘polls:detail‘ question.id %}"> {{question.question_text }} </a> </li> {% endfor %} </ul> {% else %} <p> No polls are available.</p> {% endif %}guoguos-MacBook-Pro:polls guoguo$ 2.4 再次用python manage.py collectstatic 搜集信息 guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic You have requested to collect static files at the destination location as specified in your settings: /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static This will overwrite existing files! Are you sure you want to do this? Type yes to continue, or no to cancel: yes Copying /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/static/polls/style.css 1 static file copied to /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static, 61 unmodified. 2.5 查看結果 查看 polls程序是否更新 guoguos-MacBook-Pro:mysite guoguo$ python manage.py runserver Performing system checks... 2.6 結果如下

技術分享圖片

django 設置靜態文件,static