增加app
一个网站可能有多个功能,以app为单位,模块化的管理,而不是将所有的丢到一个文件夹,
[root@centos6 mysitetest]# python manage.py startapp west [root@centos6 mysitetest]# tree west/ west/ ├── admin.py ├── __init__.py ├── models.py ├── tests.py └── views.py 0 directories, 5 files [root@centos6 mysitetest]# ls db.sqlite3 manage.py mysitetest west [root@centos6 mysitetest]# tree . ├── db.sqlite3 ├── manage.py ├── mysitetest │?? ├── __init__.py │?? ├── __init__.pyc │?? ├── settings.py │?? ├── settings.pyc │?? ├── urls.py │?? ├── urls.pyc │?? ├── views.py │?? ├── views.pyc │?? ├── wsgi.py │?? └── wsgi.pyc └── west ├── admin.py ├── __init__.py ├── models.py ├── tests.py └── views.py 2 directories, 17 files
我们还要修改项目project设置,说明我们要使用west,在mysitetest/setting.py
增加app:west
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'west', )
增加APP页面
在west/urls.py中设置URL访问对象,
修改mysitetest/urls.py:
from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'mysitetest.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^$', 'mysitetest.views.first_page'), url(r'^west/', include('west.urls')), )
在最后一行添加
url(r'^west/', include('west.urls')),
创建west/urls.py
from django.conf.urls import patterns, include, url urlpatterns = patterns('', # Examples: # url(r'^$', 'mysitetest.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^$', 'west.views.first_page'), )
在west下,修改views.py为
# -*- coding: utf-8 -*- from django.http import HttpResponse # Create your views here. def first_page(request): return HttpResponse("<p>西餐</p>")