Django学习笔记1

重点在注释#

1.views.py

 1 from django.shortcuts import render
 2 from django.http import *
 3 #from django.template import RequestContext,loader
 4 
 5 # Create your views here.
 6 
 7 def index(request):#request是已经封装好的,包括请求头
 8     # temp=loader.get_template('booktest/test.html')#loader表示加载,得到某模板
 9     # return HttpResponse(temp.render())#将模板渲染
10     return  render(request,'booktest/test.html',)#是上面两行代码的封装,简单些,
11     #'booktest/test.html'是templates下的路径,但我们还需要templates的路径,需要到setting.py下配置

2.setting.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#我猜是获得book_hero的路径
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],#路径拼接,BASE_DIR在第16行,后面加上templates,
        # C:\Users\OOK\Desktop\book_hero\templates 如果templates放在app里,则这里不用配置
        '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',
            ],
        },
    },
]

猜你喜欢

转载自www.cnblogs.com/O-OK/p/8990832.html