目录说明:
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #代表mysite目录 STATIC_URL = ‘/static/‘ STATIC_ROOT = os.path.join(BASE_DIR, ‘collected_static‘) #相当于 mysite/collected_static mysite/ ├── collected_static ├── manage.py └── mysite ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
mysite ├── manage.py ├── mysite │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── polls ├── admin.py ├── apps.py ├── __init__.py ├── urls.py ├── models.py ├── templates │ └── polls │ └── index.html └── views.py
一、setting相关设置
静态文件:
STATIC_URL = ‘/static/‘ # 把各个app下的路径搜集到该目录:python manage.py collectstatic STATIC_ROOT = os.path.join(BASE_DIR,‘static‘)
#存放共用的templates STATICFILES_DIRS = ( os.path.join(BASE_DIR, "common_static"), ‘/var/www/static/‘, )
模版文件:
全局路径
‘DIRS‘: [],#两种形式
1,(BASE_DIR, ‘templates‘)
2,/root/d/learn_models/templates
TEMPLATES = [
{
‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,
‘DIRS‘: [],
‘APP_DIRS‘: True,
‘OPTIONS‘: {
‘context_processors‘: [
‘django.template.context_processors.debug‘,
‘django.template.context_processors.request‘,
‘django.contrib.auth.context_processors.auth‘,
‘django.contrib.messages.context_processors.messages‘,
],
},
},
]在polls文件夹下建立templates文件夹,Django会自动搜索到它。
polls/templates/polls/index.html
url文件:
每个app下可以创建url,在项目url下include即可。
url include设置访问过程解析:
http://127.0.0.1:8000/polls/1/results 1,Django会先在mysite/urls.py 中找“^poll/”; 2,找到后,Django会去掉“poll/”,把剩下的“1/results”发到polls/urls.py 中继续处理; 3,在polls/urls.py 发现能匹配到“r‘^(?P<question_id>\d+)/results/$‘”,最终显示polls/views.py中的results视图;
本文出自 “LannyMa” 博客,请务必保留此出处http://lannyma.blog.51cto.com/4544390/1736118
原文地址:http://lannyma.blog.51cto.com/4544390/1736118