每个项目开始的时候,都要建立一个矿架。要建立statics,templates,并且在settings.py更改。 在statics下建立js,css,image等文件夹,并在settings.py配置。在templates下建立app_1,app_2。在settings.py中INSTALLED_APPS配置apis, app_1,app_2等等。
setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,'templates'),
],
'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',
],
},
},
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apis',
'app_1',
'app_2',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
('css', os.path.join(STATIC_ROOT, 'css').replace('\\', '/')),
('js', os.path.join(STATIC_ROOT, 'js').replace('\\', '/')),
('image', os.path.join(STATIC_ROOT, 'image').replace('\\', '/')),
)
在“工程”文件夹下建立views.py,存放主页的view。(只是为了整齐)。
在“工程”文件夹的urls.py加入include。导入apis,app_1,app_2等等的urls。
# project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^apis/', include('apis.urls')),
url(r'^app-2/', include('app_2.urls')),
url(r'^app-1/', include('app_1.urls')),
]
在templates下建立app_1,app_2。
templates
|___app_1
|___app_2
|___app_3
在每个app文件夹下建立urls.py
from django.conf.urls import url
from . import views
app_name = "app_1"
urlpatterns = [
url(r'^/xxx-1/(?P<xxx_1_id>[0-9]+)/$', views.xxx_1, name='xxx_1'),
url(r'^/xxx-2/$', views.xxx_2, name='xxx_2'),
]
每个app文件夹下建立views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.core import serializers
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
# Create your views here.
from app_2.models import SomeModel
@login_required
def xxx_1(request, xxx_1_id):
return HttpResponse("UNDER CONSTRUCTION!")
def xxx_2(request):
return HttpResponse("UNDER CONSTRUCTION!")
def xxx_3(request):
...
...
return render(request, "app_1/xxx_3.html", {"some_key_1":some_value_1, "some_key_2":some_value_2})
def xxx_4(request):
return render(request, "app_1/xxx_4.html")
启动开发服务器
python manage.py runserver 8000
刷新数据库结构
python manage.py makemigrations
同步数据库
python manage.py migrate
网友评论