視圖:
1. 在mysite/app/views.py建立路由相應(yīng)函數(shù)
from django.http import HttpResponse
def welcome(request):
? ? return HttpResponse("<h1> welcom to my tiny twitter! </h1>")
2.url映射與函數(shù)綁定 mysite/app/urls.py(新建的文件)
from django.conf.urls import url
from . import views
urlpatterns = [
? ? url(r' ',views.welcom),
]
3.在項(xiàng)目mysite/mysite/urls.py中通過include函數(shù)添加mysite/app/urls.py文件的引用
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
? ? url(r'^app/',include('app.urls')),
? ? url(r'^admin/',admin.site.urls),
]
模型類
1.修改mysite/mysite/setting.py文件中的INSTALLED_APPS數(shù)組男翰,新增app的Conf類
INSTALLED_APPS = [
? ? 'app.apps.AppConfig', ?#新增此行
]
2.mysite/app/models.py文件中定義模型類
from django.db import models
class Moment(models.Model):
? ? content = models.CharField(max_length = 200)
? ? ...
3.生成數(shù)據(jù)移植文件
python manage.py makemigrations app ?# 執(zhí)行該命令會(huì)在mysite/app/migrations文件夾中生成數(shù)據(jù)庫(kù)中間文件
4.移植到數(shù)據(jù)庫(kù)
python manage.py migrate