django学习——基础
在ubuntu下,
安装:
pip install django / sudo apt-get install python-django
项目与应用
一个project包含多个app,每个app处理不同的部分,可重用,
django-admin startproject project-name
结构如下:
project-name/
├── manage.py(控制project启动,创建app等)
└── mysite
├── __init__.py
├── settings.py(配置app)
├── urls.py(指引url到app)
└── wsgi.py
settings.py中
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'learn'
,#这是新app
)
urls.py中:
from
learn
import
views as learn_views
# 导入app的views
urlpatterns
=
[
url(r
'^$'
, learn_views.index),
# 指引url到app的views的函数
url(r
'^admin/'
, admin.site.urls),
]
django-admin startapp app-name / python manage.py startapp app-name
结构如下:
app-name/
├── __init__.py
├── admin.py
├── models.py
├── tests.py
└── views.py(每个url的请求)
总结构如下:
project-name
├── app-name
│ ├── __init__.py
│ ├── admin.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── templates
│ │ └── home.html
│ ├── tests.py
│ └── views.py
├── manage.py
└── project-name
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
启动网站:python manage.py runserver