视图:
1. 在mysite/app/views.py建立路由相应函数
from django.http import HttpResponse
def welcome(request):
return HttpResponse("<h1> welcom to my tiny twitter! </h1>")
2.url映射与函数绑定 mysite/app/urls.py(新建的文件)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r' ',views.welcom),
]
3.在项目mysite/mysite/urls.py中通过include函数添加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数组,新增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.生成数据移植文件
python manage.py makemigrations app # 执行该命令会在mysite/app/migrations文件夹中生成数据库中间文件
4.移植到数据库
python manage.py migrate