1、url
第一种,直接在根urls.py中配置
第二种,在应用中配置好后,根urls.py中引入
应用中的视图定义好方法:
from django.shortcuts import render
from django.http.response import HttpResponse
def test1(request):
return HttpResponse("<h1>测试页面1</h1>")
应用中urls.py定义路由
from django.conf.urls import url
from message.views import test1
urlpatterns = [
url(r'^test1/', test1),
]
根路由中引入应用中的路由
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^message/', include("message.urls"))
]
2、模板
应用下新建templates文件夹,新建index文件。在views.py中使用render渲染
from django.shortcuts import render
from django.http import HttpResponse
# request 接受请求的参数
def index(request):
""" return HttpResponse("hello world!")
将模板,渲染出去,render(request请求对象,模板名称,要传递给前端的数据)"""
return render(request,'index.html',{'name':'张三'})
<p>hello {{name}}!!</p>
问题,多个应用,都有index.html文件,只渲染第一个(最新django没有这个问题)
5、模型
6、admin
创建超级用户: python manage.py createsupperuser
在应用的models.py文件中引用该类
from . import models
admin.site.register(Article)
7、实例:
shell命令:
全局安装ipython ,进入到项目环境(workon) ,python manage.py shell
操作,from blog.models import Article
Article.objects.all().values()
测试用