创建应用包
在 settings.py注册和配置urls.py中要按顺序导入包名和应用名
settings.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'apps.user', 'apps.cart', 'apps.goods', 'apps.orders', )
路由urls.py
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^user$', include('apps.user.urls', namespace='user')), url(r'^cart$', include('apps.cart.urls', namespace='cart')), url(r'^orders$', include('apps.orders.urls', namespace='orders')), url(r'^$', include('apps.goods.urls', namespace='goods')), ]
如果想把apps去掉的话要在配置文件里加入搜索路径
settings.py
import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 加入搜索路径 sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) # 需要导入sys
这样就可以不加apps了
settings.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'user', 'cart', 'goods', 'orders', )
urls.py
urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^user$', include('user.urls', namespace='user')), # 用户模块 url(r'^cart$', include('cart.urls', namespace='cart')), # 购物车模块 url(r'^orders$', include('orders.urls', namespace='orders')), # 订单模块 url(r'^$', include('goods.urls', namespace='goods')), # 商品模块 ]