templates是Django指定的T目录,pycharm下templates的图标会变亮,static则可以改动。下面STATIC_URL可以指定,STATICFILES_DIRS则指定了templates的html文件的{% static 'css/style.css' %}寻址。
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
在templates的html文件中引入静态资源(js,css)
{% load staticfiles %}
......
<link href="{% static 'css/style.css' %}" rel="stylesheet" type="text/css" />
environ是一个字符串所对应环境的映像对象,类似于Windows的系统环境变量。Django之下默认根目录为项目根目录。
查看os.environ下有什么
import os
for i, v in enumerate(os.environ.items(), 1):
print(i, v)
manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "LcvSearch.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
所以当要非项目文件使用Django的模型时就要配置环境变量,记得先引入环境变量。
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djblog.settings")