django创建项目
安装django
pip install django==1.9
Note: C:\Python34\Scripts\pip.exe
创建项目
django-admin startproject myproject
执行命令生成基本目录结构
cd myproject
python manage.py startapp MyApp1
模块化
一个model一个class
,一个控制器一个class
:
删除views.py, models.py
mkdir templates/MyApp1
mkdir models
mkdir views
在views/和models/下添加 init.py
Note:
如果在views/init.py中添加:
from .myview1 import MyView1
在别的地方就可以直接从views module导入 MyView1 了
from views import MyView1
修改项目设置和url设置
project/urls.py 中include新app的urls配置
urlpatterns = [
url(r'^$', login, name="login"),
url(r'^attendence/', include('attendence.urls')),
url(r'^secu/', include('secu.urls')),
url(r'^cg/', include('cg.urls')),
]
project/settings.py中配置新app为Installed
INSTALLED_APPS = [
'App1.apps.App1Config',
'App2.apps.App2Config',
'MyApp1.apps.MyApp1Config',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
完成
可以开始业务代码了
view代码
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
class IndexView(object):
"""docstring for IndexView"""
def index(request):
return render(request, 'qba/index.html')
# return HttpResponse('Hellp HttpResponse...')
url配置
project 配置
"""dj2 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Import the include() function: from django.conf.urls import url, include
3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import url, include
# from django.contrib import admin
import qba.urls as qba_urls
# include qba urls
urlpatterns = [
url(r'^qba/', include(qba_urls)),
]
app配置
from django.conf.urls import url, include
from qba.views.index import IndexView
urlpatterns = [
url(r'index', IndexView.index, name='index'),
]